$ lookup与mongodb中的嵌套数据

泰特斯·苏蒂奥·范普拉

如何使用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"
        }
    ]
}

我希望有一个人可以帮助我。

提前致谢。

阿什

您可以尝试使用mongodb 3.4进行以下聚合

您需要在orders数组中将field(添加orders内,然后再次回滚订单到make array字段中 $unwind$addFieldsname$group

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"
  }}
])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章