【问题标题】:MongoDB aggregate with array and nested lookupMongoDB 聚合与数组和嵌套查找
【发布时间】:2021-12-31 11:36:24
【问题描述】:

我正在努力将一个查询放在一起,以完全按照我的需要聚合数据。 如果有人能提供帮助,我将不胜感激!

集合是:

集合 1

[
 {
   "_id": 1,
   "name": "Collection 1:1",
   "collection_2_ids": [5, 6]
 },
 {
   "_id": 2,
   "name": "Collection 1:2",
   "collection_2_ids": [8, 9]
 }
]

系列 2

[
 {
   "_id": 5,
   "name": "collection 2:5",
   "collection_1_id": 1,
   "collection_3_id": 12
 },
 {
   "_id": 6,
   "name": "collection 2:6",
   "collection_1_id": 1,
   "collection_3_id": 13
 },
 {
   "_id": 8,
   "name": "collection 2:8",
   "collection_1_id": 2,
   "collection_3_id": 14
 },
 {
   "_id": 9,
   "name": "collection 2:9",
   "collection_1_id": 2,
   "collection_3_id": 15
 },
]

集合 3:

[
 {
   "_id": 12,
   "name": "collection 3:12"
 },
 {
   "_id": 13,
   "name": "collection 3:13"
 },
 {
   "_id": 14,
   "name": "collection 3:14"
 },
 {
   "_id": 15,
   "name": "collection 3:15"
 }
]

我想要的是:

[
 {
   "_id": 1,
   "name": "Collection 1:1",
   "collection_2_documents": [
     {
       "_id": 5,
       "name": "collection 2:5",
       "collection_1_id": 1,
       "collection_3_id": 12,
       "collection_3_document": {
          "_id": 12,
          "name": "collection 3:12"
        }
     },
     {
       "_id": 6,
       "name": "collection 2:6",
       "collection_1_id": 1,
       "collection_3_id": 13,
       "collection_3_document": {
          "_id": 12,
          "name": "collection 3:12"
        }
     }
  ]
 },
 {
   "_id": 2,
   "name": "Collection 1:2",
   "collection_2_documents": [
       {
       "_id": 8,
       "name": "collection 2:8",
       "collection_1_id": 2,
       "collection_3_id": 14,
       "collection_3_document": {
          "_id": 14,
          "name": "collection 3:14"
        }
     },
     {
       "_id": 9,
       "name": "collection 2:9",
       "collection_1_id": 2,
       "collection_3_id": 15,
       "collection_3_document": {
          "_id": 15,
          "name": "collection 3:15"
        }
     }
   ]
 }
]

这是我当前的聚合/查找,它从集合 2 和 3 返回单独的文档数组。

[  
  {
    $lookup: {
      from: 'Collection 2',
      localField: 'collection_2_ids',
      foreignField: '_id',
      as: 'collection_2_documents'
    }
  },
  {
    $lookup: {
      from: 'Collection 3',
      localField: 'collection_2_documents.collection_3_id',
      foreignField: '_id',
      as: 'collection_3_document'
    }
  }
]

【问题讨论】:

    标签: mongodb aggregate mongodb-atlas


    【解决方案1】:

    您必须使用$lookup 阶段的pipeline 选项,并在collection2 的查找中在collection3 上使用嵌套$lookup

    db.collection1.aggregate([
      {
        "$lookup": {
          "from": "collection2",
          "let": {
            "sourceCollection_2_ids": "$collection_2_ids"
          },
          "pipeline": [
            {
              "$match": {
                "$expr": {
                  "$in": [
                    "$_id",
                    "$$sourceCollection_2_ids"
                  ]
                }
              }
            },
            {
              "$lookup": {
                "from": "collection3",
                "let": {
                  "sourceCollection_3_id": "$collection_3_id"
                },
                "pipeline": [
                  {
                    "$match": {
                      "$expr": {
                        "$eq": [
                          "$_id",
                          "$$sourceCollection_3_id"
                        ]
                      },
                      
                    }
                  },
                  
                ],
                "as": "collection_3_document"
              }
            },
            {
              "$set": {
                "collection_3_document": {
                  "$arrayElemAt": [
                    "$collection_3_document",
                    0
                  ]
                }
              }
            },
            
          ],
          "as": "collection_2_documents"
        }
      },
      
    ])
    

    Mongo Playground Sample Execution

    【讨论】:

    • 非常感谢,这太完美了!
    • @Craig 很高兴它有帮助:)
    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 2017-07-26
    • 2020-01-24
    • 1970-01-01
    • 2021-08-21
    • 2020-11-09
    • 2021-04-19
    • 2017-07-16
    相关资源
    最近更新 更多