可能填充两个级别? [英] Possible to populate two levels?

查看:69
本文介绍了可能填充两个级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有如下收藏/文档:

Say I have collections/documents like below:

问题收集:

{
   _id: ObjectId("0000"),
   title: "test question",
   survey: ObjectId("1234") //abbreviated for question assume this is a legit object id
}


调查集合:


survey collection:

{
   _id: ObjectId("1234"),
   title: "survey title!",
   user: ObjectId("5678")
}


用户集合:


user collection:

{
   _id: ObjectId("5678"),
   Name: "Abe"
}

有没有办法给我打电话:

Is there a way for me to call something like:

questions.findOne({_id: "0000"}).populate("survey.user")

获取问题的用户信息?我知道我可以填充question的直系父母,但是我很好奇能否为祖父母这样做.

to get the user information along with the question? I understand that I can populate immediate parents of question, but I'm curious if it can be done for grandparents.

推荐答案

您需要分两个步骤进行操作;首先填充survey,然后使用对 Model.populate :

You need to do it in two steps; first populating survey, and then populating survey.user using a separate call to Model.populate:

questions.findOne({_id: '0000'})
    .populate('survey')
    .exec(function(err, question) {
        questions.populate(
            question,
            { path: 'survey.user', model: 'User'},
            function(err, question) {...});
    });

这篇关于可能填充两个级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆