Mongoose聚合和分组由两个字段组成

时间:2018-01-15 15:41:01

标签: mongodb mongoose

我想聚合一个表以在其他3个表中查找,然后通过。

创建一个嵌套组

我有4个型号 订购 Order_Batches有一个ref订购 具有引用Order_Batch的批处理 具有参考批次

的事件

所以我选择所有订单然后获取所有订单,然后查找所有批次并获取该批次的所有活动

代码

let order=await Order.aggregate([
       {$lookup:{from:'orderbatches',localField:'_id',foreignField:'order',as:"order_batches"}},
       {$unwind: {path: "$order_batches", preserveNullAndEmptyArrays: true}},
       {$lookup:{from:'batches',localField:'order_batches._id',foreignField:'orderBatches',as:"batches"}},
       {$unwind: {path: "$batches", preserveNullAndEmptyArrays: true}},
       {$lookup:{from:'events',localField:'batches._id',foreignField:'batch',as:"events"}},
       {$group: {
        _id: "$_id",
        code: {$first: "$code"},
        order_batches: {$push: {
          batches: "$batches",
          events:"$events"
        }}
      }},


      ]);

输出

{
    "success": true,
    "orders": [
        {
            "_id": "5a5cbdd91ecaff0f8417a10d",
            "code": "0",
            "order_batches": [
                {
                    "batches": {
                        "_id": "5a5cbdd91ecaff0f8417a114",
                        "updatedAt": "2018-01-15T14:42:33.585Z",
                        "createdAt": "2018-01-15T14:42:33.585Z",
                        "number": 1,
                        "quantity": 10,
                        "orderBatches": "5a5cbdd91ecaff0f8417a10e",
                        "removed": false,
                        "__v": 0,

                    }
                        "events": []
                },
                {
                    "batches": {
                        "_id": "5a5cbdd91ecaff0f8417a116",
                        "updatedAt": "2018-01-15T14:42:33.586Z",
                        "createdAt": "2018-01-15T14:42:33.586Z",
                        "number": 2,
                        "quantity": 10,
                        "orderBatches": "5a5cbdd91ecaff0f8417a10e",
                        "removed": false,
                        "__v": 0,

                    }
                    "events": [
                            {
                                "_id": "5a5cbdd91ecaff0f8417a117",
                                "updatedAt": "2018-01-15T14:42:33.587Z",
                                "createdAt": "2018-01-15T14:42:33.587Z",
                                "batch": "5a5cbdd91ecaff0f8417a116",
                                "process": [
                                    "5a5cbdd91ecaff0f8417a115"
                                ],
                                "removed": false,
                                "__v": 0
                            }
                        ]
                }
            ]
        }
    ]
}

预期产出

{
    "success": true,
    "orders": [
        {
            "_id": "5a5cbdd91ecaff0f8417a10d",
            "code": "0",
            "order_batches": [
                {
                    "batches": {
                        "_id": "5a5cbdd91ecaff0f8417a114",
                        "updatedAt": "2018-01-15T14:42:33.585Z",
                        "createdAt": "2018-01-15T14:42:33.585Z",
                        "number": 1,
                        "quantity": 10,
                        "orderBatches": "5a5cbdd91ecaff0f8417a10e",
                        "removed": false,
                        "__v": 0,
                        "events": []
                    }
                },
                {
                    "batches": {
                        "_id": "5a5cbdd91ecaff0f8417a116",
                        "updatedAt": "2018-01-15T14:42:33.586Z",
                        "createdAt": "2018-01-15T14:42:33.586Z",
                        "number": 2,
                        "quantity": 10,
                        "orderBatches": "5a5cbdd91ecaff0f8417a10e",
                        "removed": false,
                        "__v": 0,
                        "events": [
                            {
                                "_id": "5a5cbdd91ecaff0f8417a117",
                                "updatedAt": "2018-01-15T14:42:33.587Z",
                                "createdAt": "2018-01-15T14:42:33.587Z",
                                "batch": "5a5cbdd91ecaff0f8417a116",
                                "process": [
                                    "5a5cbdd91ecaff0f8417a115"
                                ],
                                "removed": false,
                                "__v": 0
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

我已经完成了这项但也没有工作

let order=await Order.aggregate([
     {$lookup:{from:'orderbatches',localField:'_id',foreignField:'order',as:"order_batches"}},
     {$unwind: {path: "$order_batches", preserveNullAndEmptyArrays: true}},
     {$lookup:{from:'batches',localField:'order_batches._id',foreignField:'orderBatches',as:"order_batches.batches"}},
     {$unwind: {path: "$order_batches.batches", preserveNullAndEmptyArrays: true}},
     {$lookup:{from:'events',localField:'order_batches.batches._id',foreignField:'batch',as:"order_batches.batches.events"}},
     {$group: {
      _id: "$order_batches._id",
      // code: {$first: "$code"},
      "batches": {
        "$push": "$order_batches.batches"
      }
    }},
    {$group: {
      _id: "$_id",
      code: {$first: "$code"},
      "order_batches": {

        "$push": "$order_batches"
      }
    }},
    ]);

1 个答案:

答案 0 :(得分:0)

我得到了它的工作

 let order=await Order.aggregate([
     {$lookup:{from:'orderbatches',localField:'_id',foreignField:'order',as:"order_batches"}},
     {$lookup:{from:'styles',localField:'style',foreignField:'parent',as:"style"}},
     {$lookup:{from:'processes',localField:'style._id',foreignField:'style',as:"processes"}},
     {$unwind: {path: "$order_batches", preserveNullAndEmptyArrays: true}},
     {$lookup:{from:'batches',localField:'order_batches._id',foreignField:'orderBatches',as:"order_batches.batches"}},
     {$unwind: {path: "$order_batches.batches", preserveNullAndEmptyArrays: true}},
     {$lookup:{from:'events',localField:'order_batches.batches._id',foreignField:'batch',as:"order_batches.batches.events"}},
     {$group: {
      _id: "$order_batches._id",
      order_id: {$first: "$_id"},
      code:{$first:"$code"},
      processes:{$first:"$processes"},
      style:{$first:"$style"},

      "batches": {
        "$push": "$order_batches.batches"
      }
    }},
    {$group: {
      _id: "$order_id",
      "code":{$first:"$code"},
      "style":{$first:"$style"},
      processes:{$first:"$processes"},
      "order_batches": {

        "$push": "$batches"
      }
    }},
    ]);