可以填充两个级别吗?
Possible to populate two levels?
假设我有 collections/documents 如下所示:
题集:
{
_id: ObjectId("0000"),
title: "test question",
survey: ObjectId("1234") //abbreviated for question assume this is a legit object id
}
调查收集:
{
_id: ObjectId("1234"),
title: "survey title!",
user: ObjectId("5678")
}
用户合集:
{
_id: ObjectId("5678"),
Name: "Abe"
}
有没有办法让我调用类似的东西:
questions.findOne({_id: "0000"}).populate("survey.user")
在提问的同时获取用户信息?我知道我可以立即填充 question
的 parents,但我很好奇它是否可以用于 grandparents。
您需要分两步完成;首先填充 survey
,然后使用单独调用 Model.populate
:
填充 survey.user
questions.findOne({_id: '0000'})
.populate('survey')
.exec(function(err, question) {
questions.populate(
question,
{ path: 'survey.user', model: 'User'},
function(err, question) {...});
});
我想最好的办法是嵌套在top populate中,并使其成为一个对象
Court.
findOne({ name: 'Val' }).
populate({
path: 'events',
populate: { path: 'authorId' }
});
假设我有 collections/documents 如下所示:
题集:
{
_id: ObjectId("0000"),
title: "test question",
survey: ObjectId("1234") //abbreviated for question assume this is a legit object id
}
调查收集:
{
_id: ObjectId("1234"),
title: "survey title!",
user: ObjectId("5678")
}
用户合集:
{
_id: ObjectId("5678"),
Name: "Abe"
}
有没有办法让我调用类似的东西:
questions.findOne({_id: "0000"}).populate("survey.user")
在提问的同时获取用户信息?我知道我可以立即填充 question
的 parents,但我很好奇它是否可以用于 grandparents。
您需要分两步完成;首先填充 survey
,然后使用单独调用 Model.populate
:
survey.user
questions.findOne({_id: '0000'})
.populate('survey')
.exec(function(err, question) {
questions.populate(
question,
{ path: 'survey.user', model: 'User'},
function(err, question) {...});
});
我想最好的办法是嵌套在top populate中,并使其成为一个对象
Court.
findOne({ name: 'Val' }).
populate({
path: 'events',
populate: { path: 'authorId' }
});