Mongoose Aggregate with lookup with group

耗尽温柔 提交于 2019-12-25 09:35:37

问题


I have invoice Model as following

{
...

"itemDetails": [
            {
                "item":  "593a1a01bbb00000043d9a4c",
                "purchasingPrice": 100,
                "sellingPrice": 150,
                "qty": 200,
                "_id": "59c39c2a5149560004173a05",
                "discount": 0
            }
        ],
        "payments": [],
...

}

Item details Item is an object Id which refers to Item collection.

I wanted to get sale by Item so I manage to work it out following way

Invoice.aggregate([
    {
      "$unwind": "$itemDetails"
    }, {
      "$group": {
        "_id": "$itemDetails.item",
        "qty": {
          "$sum": "$itemDetails.qty"
        },
        "value": {
          "$sum": {
            "$multiply": [
              "$itemDetails.qty", {
                "$subtract": ["$itemDetails.sellingPrice", "$itemDetails.discount"]
              }
            ]
          }
        },
        "avarageSellingPrice": {
          "$avg": {
            "$subtract": ["$itemDetails.sellingPrice", "$itemDetails.discount"]
          }
        }
      }
    }
  ]).then(salesFigures => {
    res.status(200).json(salesFigures);
  });

This produces following result.

[
    {
        "_id": "59c89c6d68dffc0004f42a86",
        "qty": 50,
        "value": 1250,
        "avarageSellingPrice": 25
    },
    {
        "_id": "593a4bbfbbb00000043d9a54",
        "qty": 320,
        "value": 48000,
        "avarageSellingPrice": 150
    }
]

I need to get result with item Name from Item collection like

[
    {
        "_id": "59c89c6d68dffc0004f42a86",
        "itemName": "Item one",
        "qty": 50,
        "value": 1250,
        "avarageSellingPrice": 25
    },
    {
        "_id": "593a4bbfbbb00000043d9a54",
        "itemName": "Item Two",
        "qty": 320,
        "value": 48000,
        "avarageSellingPrice": 150
    }
]

I thought of using lookup before group but didn't work.

Sample documents from Invoice Collection

{
    "_id": {
        "$oid": "59c39c2a5149560004173a04"
    },
    "customer": {
        "$oid": "5935013832f9fc0004fa9a16"
    },
    "order": {
        "$oid": "59c1df8393cbba0004a0e956"
    },
    "employee": {
        "$oid": "592d0a6238880f0004637e84"
    },
    "status": "PENDING",
    "deliveryStatus": "PROCESSING",
    "created": {
        "$date": "2017-09-21T11:02:02.675Z"
    },
    "discount": 0,
    "payments": [],
    "itemDetails": [
        {
            "item": {
                "$oid": "593a1a01bbb00000043d9a4c"
            },
            "purchasingPrice": 100,
            "sellingPrice": 150,
            "qty": 200,
            "_id": {
                "$oid": "59c39c2a5149560004173a05"
            },
            "discount": 0
        }
    ],
    "__v": 0
}

Item document is like below

{
    "_id": {
        "$oid": "593a1a01bbb00000043d9a4c"
    },
    "itemCode": 1213,
    "itemName": "KIT KAT",
    "status": "active",
    "created": {
        "$date": "2017-06-09T03:46:09.445Z"
    },
    "__v": 0,
    "updated": {
        "$date": "2017-06-21T07:46:31.232Z"
    },
    "purchasingPrice": 100,
    "retailPrice": 140,
    "sellingPrice": 150
}

回答1:


You got so far, what is the hiccup? As already mentioned in the comment, you'd have to add the $lookup after your $group

{
    "$lookup": {
        from: "Item",
        localField: "_id",
        foreignField: "_id",
        as: "itemName"
    }
}

Then you'd have to $unwind since it's an array

{
    "$unwind": "$itemName",
}

And you use the final $project to retrieve the actual itemName

{
    "$project": {
        _id: 1,
        qty: 1,
        value: 1,
        avarageSellingPrice: 1,
        itemName: "$itemName.itemName"
    }
}


来源:https://stackoverflow.com/questions/46904383/mongoose-aggregate-with-lookup-with-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!