Node.js 猫鼬填充其他语法
我有两个这样的模型:Node.js 猫鼬填充其他语法,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有两个这样的模型: var UserSchema = mongoose.Schema({ name : {type: String, required: true}, image : {type: String, required: true} }); var RoomSchema = mongoose.Schema({ _user : {type: mongoose.Schema.Obje
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);
});
我知道如何像这样使用填充:
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);
});
而且效果很好,我的问题是我需要写一些类似(巫婆错了):
因为我无法访问查找房间的功能,而且我有一长串需要填充的不同对象ID。有没有解决这个问题的方法?您可以使用以下方法:
Room.findOne({_id : req.body._id}, function(err, room){
Room.populate(room, {path: '_user'}, function(err, room) {
console.log(room);
});
});
我可以发送一组具有相同语法的文档(房间而不是房间)吗?是的,这在我链接的文档中是正确的。非常感谢。。。帮我大忙!