mongodb中嵌套数据的$lookup

mongodb中嵌套数据的$lookup,mongodb,nosql,mongodb-query,aggregation-framework,Mongodb,Nosql,Mongodb Query,Aggregation Framework,如何使用mongoDB NoSQL组合2个数组对象?因为我试图在这里找到一些和我得到的相同的问题,但我没有找到与我得到的答案和问题相匹配的答案和问题 如果这里有人想帮助我,以下是我想解决的问题 示例:我尝试在mongoDB中使用noSQL,如下所示: db.tables.aggregate([ { $lookup: { from: 'reservations', localField: '_id', foreignField: 'tableId', as: 'reservation' }

如何使用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
        }
    },
]);
{ 
    "_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"
        }
    ]
}
在运行上面的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个对象数组(“订单和产品”),因此我可以得到如下结果:

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
        }
    },
]);
{ 
    "_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"
        }
    ]
}
我希望有人能帮助我


提前感谢。

您可以使用mongodb3.4在下面的聚合中进行尝试

您需要在orders数组中添加字段()

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 }}
])
对于mongodb3.6嵌套版本来说,这非常简单


您的mongodb版本是什么?mongodb版本4.0如果您使用的是mongodb 4.0,那么您应该使用第二个版本