mongoose填充其他语法
我有两个这样的模型:
var UserSchema = mongoose.Schema({ name : {type: String, required: true}, image : {type: String, required: true} }); var RoomSchema = mongoose.Schema({ _user : {type: mongoose.Schema.ObjectId, ref: 'User', required: true}, name : {type: String, required: true} });
我知道如何使用像这样的填充:
Room.findOne({_id : req.body._id}).populate('_user').exec(function(err, room){ console.log(room); });
工作正常,我的问题是,我需要写一些像( 女巫是错的 ):
Room.findOne({_id : req.body._id}, function(err, room){ room.populate('_user'); console.log(room); });
因为我无法访问查找房间的函数,而且我需要填充不同的对象ID列表。 任何想法如何解决这个问题?
你可以使用Model.populate
来做到这一点:
Room.findOne({_id : req.body._id}, function(err, room){ Room.populate(room, {path: '_user'}, function(err, room) { console.log(room); }); });