【发布时间】:2016-05-21 03:48:43
【问题描述】:
这些是我的文件:
{shopId: "aaa", customerId: "bbb", customerName: "xxx", value: 12}
{shopId: "aaa", customerId: "ccc", customerName: "yyy", value: 12}
{shopId: "aaa", customerId: "bbb", customerName: "xxx", value: 12}
{shopId: "ddd", customerId: "bbb", customerName: "xxx", value: 12}
我想知道给定客户在选定的商店里花了多少钱。
我知道怎么做:
Docs.aggregate(
[{ $match: { shopId: selectedShop } },
{
$group: {
_id: "$customerId",
totalVisits: { $sum: 1 },
totalValue: { $sum: "$value" }
}
}
], function (err, result) {
if (err) {
//
} else {
//
}
}
);
问题是我得到的结果包含_id: "$customerId" 字段,我想得到customerName 并隐藏customerId。
有可能吗?
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query aggregation-framework