【问题标题】:$lookup with nested data in mongodb$lookup 与 mongodb 中的嵌套数据
【发布时间】:2018-08-04 14:55:59
【问题描述】:

如何使用 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版本是多少?
  • mongodb 4.0版

标签: mongodb nosql mongodb-query aggregation-framework


【解决方案1】:

您可以尝试以下聚合与 mongodb 3.4

你需要 $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"
  }}
])

【讨论】:

  • 好吧,如果您使用的是 mongodb 4.0,那么您应该使用第二个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多