通过$ mergedObject查询在$ lookup上加入另一个集合

时间:2018-08-28 05:34:22

标签: javascript node.js mongodb

我有这个查询,它使用MongoDB的左连接。我想添加另一个$ lookup,但是当我将它放在具有$ mergeObjects的$ lookup之前时,似乎无法使它工作。

db.getCollection("coverages").aggregate([
        {
        $lookup: {
            from: "filings",
            let: { coverageRepoNo: "$RepoNo" },
            pipeline: [
                {
                    $match: {
                        $expr: { $eq: ["$RepoNum", "$$coverageRepoNo"] }
                    }
                },
                { $sort: { PeriodEndDate: -1, SubType: 1 } },
                { $group: { _id: "$RepoNum", data: { $first: "$$ROOT" } } },
                { $replaceRoot: { newRoot: "$data" } }
            ],
            as: "fromItems"
        }
    },
    {
        $replaceRoot: {
            newRoot: {
                $mergeObjects: [
                    { $arrayElemAt: ["$fromItems", 0], },
                    "$$ROOT"
                ]
            }
        }
    },
    { $project: { fromItems: 0, __v: 0, _id: 0 } }
])

我想添加另一个$ lookup。我将$ lookup放在这些属性的哪里?

$lookup: {
            from: "analysts",
            localField: "AnalystID", // field in the coverages collection
            foreignField: "AnalystID", // field inthe analysts collection
            as: "fromAnalysts"
        }

嘿@AnthonyWinzlet是我将查询放在第一行时的示例数据

{ 
"AnalystID" : "JDO", 
"AnalystName" : "John Doe", 
"AnalystLocationName" : "California", 
"fromAnalysts" : [
    {
        "_id" : ObjectId("5b84a79135da280d88858a13"), 
        "AnalystID" : "JDO", 
        "AnalystName" : "John Doe", 
        "AnalystLocationName" : "California",
        "TLName": "Foo Bar"
    }
]}

我只希望“ TLName”显示在“ AnalystLocationName”之后。我该怎么办?

0 个答案:

没有答案