这是我的场景:保存倍数的对象在一次蒙戈(多对多猫鼬)
我有2个集合(表):项目和学生(的关系是多对多)。
我的车型有:
项目:
var ProjectSchema = new Schema({ name: { type: String }, description: { type: String }, user : { type : Mongoose.Schema.Types.ObjectId, ref : "User" } }); Mongoose.model('Project', ProjectSchema);
用户:
var Userchema = new Schema({ name: { type: String }, surname: { type: String }, project : { type : Mongoose.Schema.Types.ObjectId, ref : "Project" } }); Mongoose.model('User', UserSchema);
在我看来,我有2个输入,其中接收名称和描述项目,然后有多个小组ct选择1个或多个用户为这个项目。当我单击确定时应该:
- 创建项目,并向用户添加所选用户作为BBDD中的对象。
- 更新用户,使用项目协调。
我试了下,但只有funcion如果我选择1个用户:
exports.addUserProject = function(req, res) {
var project=new svmp.Project();
project.name=req.body.name;
project.description=req.body.description;
project.user=req.body.user[0]._id;
project.save(function(err,project){
if (err) {
return res.send(400, {
message : getErrorMessage(err)
});
} else {
res.jsonp(project);
}
})
};
我BBDD结果是下一个:
{ “_id”:物件( “57a200a38fae140913ac5413” ), “用户”:物件( “578f41ddb0641d961416c3f5”), “名”: “PROJECT1”, “说明”: “PROJECT1说明”, “__ v”:0}
感谢您的帮助
谢谢,为我完美工作。 – JossephBot