2017-01-27 51 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