[英]how to work with 2 schemas, how to save correctly Mongoose, nodejs, express
嘿,我正在嘗試保存到2個模式中並返回結果,問題是保存,我認為,我已經覆蓋了需要填充以便檢索數據的部分,但是沒有保存該部分。太棒了,如果有人可以指出我的方向,即如何保存到彼此“符號鏈接”的不同架構中
//userlist.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
DataSchema = require('../models/data');
var UserSchema = new Schema({
fullname: String,
username: { type: String, unique: true },
password: { type: String },
admin: Boolean,
location: String,
email: String,
meta: [{
age: Number,
gender: String
}],
dataSchema: [DataSchema], /// this i dont know if it works or not
data: {
type: Schema.Types.ObjectId,
ref: 'Data'
},
created_at: Date,
updated_at: Date
});
module.exports = mongoose.model('Userlist', UserSchema);
//data.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var DataSchema = new Schema({
favoritter: String,
website: String,
homemade: String,
image: String,
user: {
type: Schema.Types.ObjectId,
ref: 'Userlist'
}
});
module.exports = mongoose.model('Data', DataSchema);
// userRoutes.js
exports.addUserAPI = function(req, res) {
var user = new Userlist();
user.fullname = req.body.fullname,
user.username = req.body.username,
user.admin = req.body.admin,
user.location = req.body.location,
user.password = req.body.password,
user.email = req.body.email,
user.age = req.body.age,
user.favoritter = req.body.favoritter,
user.website = req.body.website,
user.image = req.body.image,
user.homemade = req.body.homemade;
user.save(function(err, users) {
if (err)
res.send(err)
Userlist.populate(users, {path: 'data'}, function(err, data){
return res.send(data);
});
});
};
exports.findAllAPI = function(req, res) {
Userlist.find({})
.populate({ path: 'meta' })
.populate({ path: 'data'})
.exec(function(err, users) {
if (err)
res.send(err);
console.log(JSON.stringify(users, null, "\t"))
res.json(users);
});
};
{
//安慰
"_id": "56ea6df2419e900ac9d2e4fc",
"created_at": "2016-03-17T08:42:26.837Z",
"updated_at": "2016-03-17T08:42:26.837Z",
"password": "$2a$1",
"email": "kenny@blah.dk",
"admin": true,
"username": "blahblah",
"fullname": "Kenny",
"datalist": [],
"__v": 0,
"dataSchema": [],
"meta": []
}
如您所見,由於某種原因,數據列表中的noir dataSchema和meta都沒有可視化...當我嘗試通過控制台直接從Data中檢索時,那里什么也沒有。 僅在用戶列表中,我有一些用戶。
這就是我想要的樣子
"_id": "56ea6df2419e900ac9d2e4fc",
"created_at": "2016-03-17T08:42:26.837Z",
"updated_at": "2016-03-17T08:42:26.837Z",
"password": "$2a$1",
"email": "kenny@blah.dk",
"admin": true,
"username": "blahblah",
"fullname": "Kenny",
"datalist": [
"_id": "56ea6df2419e900ac9d2e"
],
"__v": 0,
"dataSchema": [
"favoritter": "www"
"homemade": "yes"
"website": "http://"
"image": "/file/asd.jpeg"
],
"meta": [
"age": "32"
"location": "Finland"
]
}
我創建了兩個模式,第一個是person,第二個是包含對person的引用的地址。 希望這對您有所幫助。
模式一
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var personSchema = new Schema({
name: String
},{collection:'person'});
module.exports = mongoose.model('person',personSchema);
模式二
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var addressSchema = new Schema({
address:String,
postalCode:Number,
personid : {type:ObjectId, ref:'person'}
},{collection:'address'});
module.exports = mongoose.model('address',addressSchema);
在保存時
// Schema obj
var personSchema = mongoose.model('person',person.personSchema);
var addressSchema = mongoose.model('address',address.addressSchema);
var newPerson = new personSchema({name:'yourname'});
newPerson.save();
var newAddress = new addressSchema({address:'yourAddress',postalCode:666,personid:newPerson._id});
newAddress.save();
在獲取時
addressSchema.find().populate('personid').exec(function (err,res) {
if(err){
console.log('error');
}
else{
console.log(res);
// or just name
console.log(res.personid.name);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.