【问题标题】:Aggregate, lookup and addField from nested array从嵌套数组聚合、查找和添加字段
【发布时间】:2020-05-06 13:46:45
【问题描述】:

我在使用 MongoDB 查询时遇到问题。 首先,我有一个结构如下的“testScriptResultCollection”:

{
    testCaseId: x
    testScriptId: 1
    descripttion: aaa

}
{
    _id: 2
    testCaseId: x
    testScriptId: 2
    descripttion: bbb
}
{
    _id: 3
    testCaseId: x
    testScriptId: 3
    descripttion: ccc
}

另一个集合是"testCaseCollection"

{
    _id: 1
    testCaseId: x
    testScripts: [
        {
            testScriptId: 1
            name: testScript1_Name
        },
        {
            testScriptId: 2
            name: testScript2_Name
        }
        {
            testScriptId: 3
            name: testScript3_Name
        }
    ]
}

我需要提取一个像这样的对象:

 [ 
        {
            testCaseId: x
            testScriptId: 1
            descripttion: aaa
            name: testScript1_Name
        },
        {
            testCaseId: x
            testScriptId: 2
            descripttion: bbb
            name: testScript2_Name
        },
        {
            testCaseId: x
            testScriptId: 3
            descripttion: ccc
            name: testScript3_Name
        },
    ]

我曾尝试查询以使用 "testCaseId" 查找 2 个集合,并找到类似 testScriptId 的 "name",但它出错了

testScriptResultCollection.aggregate{[
    {
        $match: {testCaseId : x}
    },
    {
            $lookup:
            {
                from: "testCaseCollection"
                localField: "testCaseId",
                foreignField: "testCaseId",
                as: "combineResults"
            }
    },
    {
        $addFields : 
        {
            "name": {
                $filter: { 
                    input: "$combineResults.testScripts",
                    as: "testScriptArr",
                    cond: { $eq: ["$$testScriptArr.testScriptId", $testScriptId]}
                }
            }
        }
    }
]}

谁能帮帮我。任何帮助,将不胜感激。非常感谢。

【问题讨论】:

  • 你好你可以试试这个查询,但这不是最好的方法:docs.google.com/document/d/…
  • 答案有问题吗?
  • 不是最好的方法,但可以正常工作,感谢您的帮助。

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

你可以试试下面的聚合

基本上您需要使用$mergeObjects 来合并两个集合对象,最后使用$replaceRoot 将它们转移到顶层。

db.getCollection('testCaseCollection').aggregate([
  { "$unwind": "$testScripts" },
  { "$lookup": {
    "from": "testScriptResultCollection",
    "localField": "testScripts.testScriptId",
    "foreignField": "testScriptId",
    "as": "newField"
  }},
  { "$unwind": "$newField" },
  { "$replaceRoot": { "newRoot": { "$mergeObjects": ["$newField", "$testScripts"] }}}
])

【讨论】:

  • 我需要通过 testScriptId 获取 'name',所以,这个答案并没有解决我的问题,但无论如何,谢谢你的帮助,我也学到了关于 $replaceRoot 和 $mergeObjects 的新东西跨度>
  • 这里是工作示例mongoplayground.net/p/7xMPygiSX1o
  • 在示例中完美运行,但我还需要将“testScriptResultCollection”加入其他集合,这意味着查询应该以 db.testScriptResultCollection.aggregate([ ... ]) 开头。你能帮我写一个查询,从“testScriptResultCollection”通过“testCaseId”连接到“testCaseCollection”,并通过“testScriptId”获取“名称”吗?谢谢。
  • 好的,那么请使用适当的示例文档和输出提出一个新问题。我一定会回答的
猜你喜欢
  • 2021-12-31
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 2021-08-21
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
相关资源
最近更新 更多