$ lookup与mongodb中的嵌套数据

时间:2018-08-04 14:56:00

标签: mongodb nosql mongodb-query aggregation-framework

如何使用mongoDB NoSQL合并2个数组对象?因为我试图在这里找到一些与我所遇到的问题相同的问题,但是我没有找到与我所得到的问题相匹配的答案和问题。

如果这里有人想要帮助我,这就是我要解决的问题。

示例:我尝试在mongoDB中使用noSQL,如下所示:

db.tables.aggregate([
    { $lookup: { from: 'reservations', localField: '_id', foreignField: 'tableId', as: 'reservation' }},
    { $unwind: { path: '$reservation', 'preserveNullAndEmptyArrays': true }},
    { $lookup: { from: 'orders', localField: 'reservation._id', foreignField: 'reservationId', as: 'orders' }},
    { $lookup: { from: 'products', localField: 'orders.productId', foreignField: '_id', as: 'products' }},
    {
        $project: {
            '_id': 1,
            'initial': 1,
            'description': 1,
            'reservation._id': 1,
            'reservation.guest': 1,
            'orders._id': 1,
            'orders.status': 1,
            'orders.quantity': 1,
            'orders.productId': 1,
            'products._id': 1, 
            'products.name': 1
        }
    },
]);

运行上面的noSQL mongoDB之后,我得到以下结果:

{ 
    "_id" : ObjectId("5b63e519514cf01c2864749a"), 
    "description" : "Kursi VIP 01", 
    "reservation" : {
        "_id" : ObjectId("5b63f104514cf01c286474b6"), 
        "guest" : "Jhon Doe"
    }, 
    "orders" : [
        {
            "_id" : ObjectId("5b63f239514cf01c286474bb"), 
            "productId" : ObjectId("5b63e72d514cf01c286474a3"), 
            "status" : "3", 
            "quantity" : "2"
        }, 
        {
            "_id" : ObjectId("5b63f252514cf01c286474bc"), 
            "productId" : ObjectId("5b63e7de514cf01c286474a6"), 
            "status" : "2", 
            "quantity" : "3"
        }, 
        {
            "_id" : ObjectId("5b63f267514cf01c286474bd"), 
            "productId" : ObjectId("5b63e937514cf01c286474ac"), 
            "status" : "0", 
            "quantity" : "2"
        }
    ], 
    "products" : [
        {
            "_id" : ObjectId("5b63e72d514cf01c286474a3"), 
            "name" : "AQUA 600ML"
        }, 
        {
            "_id" : ObjectId("5b63e7de514cf01c286474a6"), 
            "name" : "Nasi Goreng Kecap Asin"
        }, 
        {
            "_id" : ObjectId("5b63e937514cf01c286474ac"), 
            "name" : "Daging Ayam Goreng"
        }
    ]
}

现在,我的问题是。 如何合并/组合2个对象数组(“订单和产品”),这样我就可以获得以下结果:

{ 
    "_id" : ObjectId("5b63e519514cf01c2864749a"), 
    "description" : "Kursi VIP 01", 
    "reservation" : {
        "_id" : ObjectId("5b63f104514cf01c286474b6"), 
        "guest" : "Jhon Doe"
    }, 
    "orders" : [
        {
            "_id" : ObjectId("5b63f239514cf01c286474bb"), 
            "productId" : ObjectId("5b63e72d514cf01c286474a3"), 
            "name" : "AQUA 600ML",
            "status" : "3", 
            "quantity" : "2"
        }, 
        {
            "_id" : ObjectId("5b63f252514cf01c286474bc"), 
            "productId" : ObjectId("5b63e7de514cf01c286474a6"), 
            "name" : "Nasi Goreng Kecap Asin",
            "status" : "2", 
            "quantity" : "3"
        }, 
        {
            "_id" : ObjectId("5b63f267514cf01c286474bd"), 
            "productId" : ObjectId("5b63e937514cf01c286474ac"), 
            "name" : "Daging Ayam Goreng"
            "status" : "0", 
            "quantity" : "2"
        }
    ]
}

我希望有人可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用mongodb 3.4

进行以下聚合

您需要 $unwind订单数组以在订单内添加字段($addFieldsname,然后$group将订单再次回滚到make数组字段< / p>

db.tables.aggregate([
  { "$lookup": {
    "from": "reservations",
    "localField": "_id",
    "foreignField": "tableId",
    "as": "reservation"
  }},
  { "$unwind": { "path": '$reservation', 'preserveNullAndEmptyArrays': true }},
  { "$lookup": {
    "from": "orders",
    "localField": "reservation._id",
    "foreignField": "reservationId",
    "as": "orders",
  }},
  { "$unwind": { "path": '$orders', 'preserveNullAndEmptyArrays': true }},
  { "$lookup": {
    "from": "products",
    "localField": "orders.productId",
    "foreignField": "_id",
    "as": "orders.products"
  }},
  { "$unwind": { "path": '$orders.products', 'preserveNullAndEmptyArrays': true }},
  { "$addFields": {
    "orders.name": "$orders.products.name"
  }},
  { "$group": {
    "_id": "$_id",
    "description": { "$first": "$description" },
    "reservation": { "$first": "$reservation" },
    "orders": { "$push": "$orders" }
  }},
  { "$project": { "orders.products": 0 }}
])

使用mongodb 3.6 嵌套$lookup版本

,这非常简单
db.tables.aggregate([
  { "$lookup": {
    "from": "reservations",
    "let": { "reservationId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$tableId", "$$reservationId" ] } } }
    ],
    "as": "reservations"
  }},
  { "$lookup": {
    "from": "orders",
    "let": { "reservationId": "$reservation._id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$reservationId", "$$reservationId" ] } } },
      { "$lookup": {
        "from": "products",
        "let": { "productId": "$productId" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$_id", "$$productId" ] } } },
          { "$project": { "_id": false }}
        ],
        "as": "products"
      }},
      { "$unwind": "$products" },
      { "$addFields": { "name": "$products.name" } },
      { "$project": { "products": 0 }}
    ],
    "as": "orders"
  }}
])