如何使用展开来获取多个文档及其多个文档(链)?

时间:2019-10-14 10:41:04

标签: node.js mongodb express mongoose

我正在使用Node和MongoDB创建一个项目。该数据库包含多个集合和文档。Project表具有多个记录,并且有许多程序。每个程序都有许多发言者。当我使用以下代码时,它仅显示一个程序的发言人

尝试过的代码

function getOne(req,res,next)
{
    try
    {          
        var aggregate = Project.aggregate();

        aggregate.match({"_id":ObjectId(id)})
        .lookup({ from: "programmes", localField: "_id", foreignField: "Project_id",as: "programme_doc"})
        .unwind("$programme_doc")
        .lookup({ from: "gust_speakers", localField: "programme_doc._id", foreignField: "programme_id",as: "programme_doc.speakers"})
        .lookup({ from: "status", localField: "Project_status_id", foreignField: "_id",as: "status_doc"})
        .lookup({ from: "guests", localField: "_id", foreignField: "Project_id",as: "guest_doc"})
        .lookup({ from: "second_manager", localField: "_id", foreignField: "Project_id",as: "team_doc"})
        .lookup({ from: "users", localField: "team_doc.user_id", foreignField: "_id",as: "team_docc"})
        .lookup({ from: "type", localField: "Project_category_id", foreignField: "_id",as: "category_doc"})
        .lookup({from: "feedbacks", localField: "_id", foreignField: "Project_id",as: "feedbacks"})
        .lookup({from: "feedbacks", localField: "_id", foreignField: "programme_doc._id",as: "pro_feedbacks"})
        .lookup({from: "links", localField: "_id", foreignField: "Project_id",as: "link_doc"       

        Project.aggregatePaginate(aggregate, options, function(err, results, pageCount, count)
        {
            Status.find({}).select("_id name color").exec(function(err, statusResult) {
                    if (!err) {
                        ProjectStatus.find({}).select("_id name dp").exec(function(err, eveResult) {

                            if (!err) {
                                return res.status(200).json({data:results[0],pro_status:statusResult,pro_status:proResult});
                            }
                        });
                    }
                });
        }) 
    }
    catch (err)
    {
        return res.status(500).json(err);
    }
}

示例输出

{
    "data": {        
        },
    "programme_doc": {
        // program details
        ,
        "speakers": [
            {
                //speaker 1
            },
            {
                //speaker 2
            }
        ]
    },
}

但是,如果我对unwind和下一个lookup行进行如下注释,它将显示所有节目详细信息,没有演讲者详细信息。

aggregate.match({"_id":ObjectId(id)})
.lookup({ from: "programmes", localField: "_id", foreignField: "Project_id",as: "programme_doc"})
//.unwind("$programme_doc")
//.lookup({ from: "gust_speakers", localField: "programme_doc._id", foreignField: "programme_id",as: "programme_doc.speakers"})
.lookup({ from: "status", localField: "Project_status_id", foreignField: "_id",as: "status_doc"})

//rest of codes

输出

{
    "data": {        
        },
    "programme_doc": [
        {
            // program details
        },
        {
            // program details
        },
        {
            // program details
        },
        {
            // program details
        },
    ]
}

0 个答案:

没有答案