2017-01-27 38 views
0

您能否帮助我。 我搜索了互联网,找不到任何解决方案。 如何为小孩创建查询以使用父项的参数?为孩子创建查询以使用父母的参数

var Photos = new Schema({ 
    photo_id: {type: String, required: true}, 
    photo_path_low: { type: String, required: true } 
}); 

var Users = new Schema({ 
    user_id: { type: String, required: true }, 
    count_coins: { type: Number, default: 20 }, 
    photos_relation: [Photos] 
}); 
... 
... some code 
... 
PhotoModel.findOne().where('parent.count_coins').gt(1)..... // parent for Example 
+0

您的照片方案没有提及用户。 – Paul

+0

如何添加对用户的引用?举个例子,如果不是困难的话。 – user3424119

+0

阅读文档它包含您需要的所有内容 –

回答

1

对于这种情况,有对象的引用:

var Photos = new Schema({ 
    photo_id: {type: String, required: true}, 
    photo_path_low: { type: String, required: true } 
    createdBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, 
}); 

然后,当你让你的查询,你可以填充引用这样的:

Photo.findOne({_id: 123}) 
.populate('createdBy') 
.exec(function(err, post) { 
    // do stuff with post 
}); 

你可以找到更多的this mongoose documentation