[英]how to work with 2 schemas, how to save correctly Mongoose, nodejs, express
Hej there, i'm trying to save into 2 schemas and get back a result, the problem is saving i think, i got the part covered where i need to populate in order to retrieve data, but not the part on how to save to it Sooo yea if anyone could point my in a direction on how to save into different schemas that are "symlinked" to each other 嘿,我正在尝试保存到2个模式中并返回结果,问题是保存,我认为,我已经覆盖了需要填充以便检索数据的部分,但是没有保存该部分。太棒了,如果有人可以指出我的方向,即如何保存到彼此“符号链接”的不同架构中
//userlist.js //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 //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 // 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);
});
};
{
//console //安慰
"_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": []
} }
as you can see there is nothing visual in datalist noir dataSchema and neither meta for some reason... when i try to retrieve directly from Data via console there is nothing there. 如您所见,由于某种原因,数据列表中的noir dataSchema和meta都没有可视化...当我尝试通过控制台直接从Data中检索时,那里什么也没有。 only in userlist i have some users. 仅在用户列表中,我有一些用户。
This is how i want it to look like 这就是我想要的样子
"_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"
]
} }
I have created two schema first is person and second is address which contains the reference to person. 我创建了两个模式,第一个是person,第二个是包含对person的引用的地址。 Hope this helps to you. 希望这对您有所帮助。
Schema One 模式一
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var personSchema = new Schema({
name: String
},{collection:'person'});
module.exports = mongoose.model('person',personSchema);
Schema Two 模式二
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);
While saving 在保存时
// 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();
While fetching 在获取时
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.