MongoDb $查找数组字段中的嵌套文档

MongoDb $lookup nested document in array field

我有一个带有卡片数组的看板集合

示例看板文档:

{
"cards": [{
    "rank": 0,
    "_id": {
        "$oid": "5fc7a1ac5591fa0078d01473"
    },
    "orderId": {
        "$oid": "5fb3bfd12854e63840477b1b"
    },
    "kanbanCol": "Backlog"
}, {
    "rank": 0,
    "orderId": {
        "$oid": "5fb4130bd8db1c5818993ea4"
    },
    "kanbanCol": "CNC"
}],
"columns": [{
    "headerText": "Backlog",
    "keyField": "Backlog"
}, {
    "headerText": "To Do",
    "keyField": "To Do"
}, {
    "headerText": "Doing",
    "keyField": "Canteadora"
}, {
    "headerText": "Done",
    "keyField": "Done"
}],
"title": "Maquinas2",
"keyField": "kanbanCol",
"ownerId": {
    "$oid": "5e25c5126230b32758c52b61"
},
"creationDate": {
    "$date": "2020-12-01T15:29:31.494Z"
},
"updatedAt": {
    "$date": "2020-12-02T15:11:53.327Z"
},
"__v": 5

每个看板都有一个卡片字段类型:[Card]

这是卡片元素的示例:

{
    "rank": 0,
    "_id": {
        "$oid": "5fc7a1ac5591fa0078d01473"
    },
    "orderId": {
        "$oid": "5fb3bfd12854e63840477b1b"
    },

正如我们在示例中看到的,每个卡片对象都有一个 orderId 字段,此 ID 属于我的订单集合中的一个订单文档。

我的预期结果是将整个看板元素替换为从我的订单集合中填充的订单对象的卡片的 orderId 字段,如下所示:

{
"cards": [{
    "rank": 0,
    "_id": {
        "$oid": "5fc7a1ac5591fa0078d01473"
    },
    "order": {
        
                "_id": "5fb3bfd12854e63840477b1b",
                "orderNumber": 87,
                "name": "test3",
                "ref": "23",
                "factory": "Factory A",
                "owner": "Owner 1",
                "status": "in factory",
                "creationDate": "2020-11-17T12:19:52.685Z",
                "updatedAt": "2020-11-30T13:10:43.567Z",
                "__v": 0,
                "orderId": "004_00087",
                "comments": "",
                "factoryId": "5e25c5126230b32758c52b61",
                "ownerId": "5f71bc3f292775001fbb528b"
            

    },
    "kanbanCol": "Backlog"
}, 
{
    "rank": 0,
    "order": {
        "_id": "5fb3bfd12854e6384048usd1d",
                "orderNumber": 88,
                "name": "test4",
                "ref": "24",
                "factory": "Factory A",
                "owner": "Owner 2",
                "status": "finished",
                "creationDate": "2020-11-17T12:19:52.685Z",
                "updatedAt": "2020-11-30T13:10:43.567Z",
                "__v": 0,
                "orderId": "004_00088",
                "comments": "",
                "factoryId": "5e25c5126230b32758c52b61",
                "ownerId": "5f71bc3f292775001fls321b1d"
    },
    "kanbanCol": "CNC"
}],
"columns": [{
    "headerText": "Backlog",
    "keyField": "Backlog"
}, {
    "headerText": "To Do",
    "keyField": "To Do"
}, {
    "headerText": "Doing",
    "keyField": "Canteadora"
}, {
    "headerText": "Done",
    "keyField": "Done"
}],
"title": "Maquinas2",
"keyField": "kanbanCol",
"ownerId": {
    "$oid": "5e25c5126230b32758c52b61"
},
"creationDate": {
    "$date": "2020-12-01T15:29:31.494Z"
},
"updatedAt": {
    "$date": "2020-12-02T15:11:53.327Z"
},
"__v": 5

 

您可以使用 $lookup 获取订单数据,例如:-

db.getcollection().aggregate([{$unwind:"$cards"},{ "$lookup": {
"from": orders,
"let": { "orderId": "$cards.orderId.oid" },
"pipeline": [
   { "$match": { "$expr": { "$eq": [ "$_id", "$$orderId" ] } } }
 ],
 "as": "orders" 
}},{$unwind:"$orders"},{$group:{_id:null,root:{$mergeObjects:'$$ROOT' },cards:{$push:"$cards"}}},{
    $replaceRoot: {
        newRoot: {
            $mergeObjects: ['$root', '$$ROOT']
        }
    }
}])

要获得更多许可,您可以访问此 url 解决方案与所提问题类似。 http://www.petecorey.com/blog/2020/01/29/mongodb-object-array-lookup-aggregation/

这是我的最终解决方案:

aggregate([
        {
          '$match': {
            '_id': Types.ObjectId(kanbanId)
          }
        }, {
          '$unwind': {
            'path': '$cards', 
            'preserveNullAndEmptyArrays': true
          }
        }, {
          '$lookup': {
            'from': 'orders', 
            'let': {
              'order_id': '$cards.orderId'
            }, 
            'pipeline': [
              {
                '$match': {
                  '$expr': {
                    '$eq': [
                      '$_id', '$$order_id'
                    ]
                  }
                }
              }
            ], 
            'as': 'cards.order'
          }
        }, {
          '$unwind': {
            'path': '$cards.order', 
            'preserveNullAndEmptyArrays': true
          }
        }, {
          '$group': {
            '_id': '$_id', 
            'root': {
              '$mergeObjects': '$$ROOT'
            }, 
            'cards': {
              '$push': '$cards'
            }
          }
        }, {
          '$replaceRoot': {
            'newRoot': {
              '$mergeObjects': [
                '$root', '$$ROOT'
              ]
            }
          }
        }, {
          '$project': {
            'root': 0
          }
        }
      ])