Mongoose聚合与组查找

时间:2017-10-24 07:15:38

标签: node.js mongodb mongoose aggregation-framework

我发票模型如下

{
...

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

}

项目详细信息项目是引用项目集合的对象ID。

我想通过Item销售,所以我设法按照方式进行销售

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);
  });

这会产生以下结果。

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

我需要从Item集合中获取项目名称的结果,如

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

我想过在分组之前使用查找但是没有工作。

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
}

项目文件如下

{
    "_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 个答案:

答案 0 :(得分:2)

你到目前为止,什么是打嗝? 正如评论中已经提到的那样,您必须在$lookup

之后添加$group
{
    "$lookup": {
        from: "Item",
        localField: "_id",
        foreignField: "_id",
        as: "itemName"
    }
}

然后你必须$unwind,因为它是一个数组

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

您使用最终的$project来检索实际的 itemName

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