猫鼬嵌套参考种群 [英] Mongoose nested reference population
问题描述
我无法理解 mongoose 的 populate 方法背后的一些概念.我首先采用了嵌入式方法,但由于担心大量数据开销和不同步的文档,我尝试将范式更改为 ref
其他文档.
I'm having trouble understanding some of the concepts behind mongoose's populate methods. I had an embedded approach working first, although, as I feared a big overhead of data and out-of-sync documents going around I tried changing the paradigm to ref
other documents.
我的架构类似于以下(删除了不相关的属性):
My Schema is similar to the following (removed irrelevant properties):
var userSchema = mongoose.Schema({
name: {type: String, default:''},
favorites:{
users:[{type: Schema.Types.ObjectId, ref: this}],
places:[{type: Schema.Types.ObjectId, ref: PlaceSchema}]
}
});
module.exports = mongoose.model('User', userSchema);
现在我正在尝试像这样获取用户
的收藏夹
:
Now I'm trying to get a User
's favorites
like this:
User.findOne({_id: currentUser}).exec(function(err, user){
console.log(user);
if (err)
throw err;
if(!user){
console.log("can't find user");
}else{ // user found
user.populate('favorites.users');
user.populate('favorites.places');
// do something to the 'user.favorites' object
}
});
虽然这没有按预期工作,因为 user.favorites.users
和 user.favorites.places
都未定义.
Although this doesn't work as intended, as both user.favorites.users
and user.favorites.places
come up undefined.
我认为我可以按上述方式填充,但显然,情况并非如此.从我读到的内容来看,我一定遗漏了一些指示(可能)ref
文档的模型的东西?这个流程对我来说很新,我有点迷茫.
I thought that I could populate as above, but apparently, that's not the case. From what I read, I must be missing something indicating (maybe) the model of the ref
'ed document? This flow is very new to me and I'm kinda lost.
无论如何我可以通过填充我的查询结果来获得 users
和 places
的数组吗?我尝试将 populate
与 exec
链接起来,但它也不起作用.有没有更好的方法来达到这个结果?
Is there anyway I can get an array of users
and places
by populating my query result as above? I tried populate
chained with exec
and it doesn't work either. Is there a better way to achieve this result?
如果需要,在数据库中,User
文档显示为:
In case it's needed, in the DB, a User
document shows up as:
{
"_id": "56c36b330fbc51ba19cc83ff",
"name": "John Doe",
"favorites": {
"places": [],
"users": [
"56b9d9a45f1ada8e0c0dee27"
]
}
}
更多细节......我目前正在存储/删除这样的引用 ObjectIds(注意 targetID 是一个字符串):
A couple more details... I'm currently storing/removing the reference ObjectIds like this (note targetID is a String):
user.favorites.users.push({ _id: mongoose.Types.ObjectId(targetID)});
user.favorites.users.pull({ _id: mongoose.Types.ObjectId(targetID)});
另外,我需要用它们各自的文档以及填充users
和places
,我认为这在我的原始文件中可能不清楚问题.
Also, I need to populate the users
and places
with their respective documents aswell, I think that might not be clear in my original question.
推荐答案
好吧,我通过适当关注文档(以及@DJeanCar 的 (+1'd) 帮助/指针)找到了我需要的东西.
Well, I figured out what I needed by paying proper attention to the docs (and also with @DJeanCar 's (+1'd) help/pointers).
根据 Mongoose 的 docs 关于跨多个级别填充,我'已经达到了这个解决方案:
By Mongoose's docs regarding Populating across multiple levels, I've reached this solution:
User.findOne({_id: currentUser})
.populate({path:"favorites.users", populate: "name"})
.populate({path:"favorites.places", populate: "name"})
.exec(function(err, user){
if(err)
throw err;
if(!user){
console.log("couldnt find source user");
}else{
// here, user.favorites is populated and I can do what I need with the data
}
});
此外,据我所知,您还可以在 populate()
的选项中传递 select: "field field field"
,如果您需要过滤填充后所需的文档字段.
Also, from what I could tell, you can also pass select: "field field field"
in populate()
's options, should you need to filter the document fields you require after population.
希望这对有类似问题的人有所帮助!
Hope this helps someone with similar issues!
这篇关于猫鼬嵌套参考种群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!