我想从user_id
组合user_id
order
其中有很多条目,但我想获取订单数量最多的5位用户,并计算特定用户的总价。
例如,如果我发出了10个订单,我想获取我的ID和我所有订单的总和。 我用下面的代码,但它不工作:
Order.find({$group : { user_id : "$user_id"}},function(error,fetchAllTopUsers){
console.log('##################');
console.log(fetchAllTopUsers);
})
我已经检查了这个例子,但他们没有使用与$group
找到。我在Node.js和MongoDB中是新的。
Query and sum all with mongoose
样本文件
{ user_id : '57c7f4312b3c9771219bd21c', totalprice : 100 },
{ user_id : '57c7f4312b3c9771219bd21c', totalprice : 59 },
{ user_id : '57c7f4312b3c9771219bd21c', totalprice : 26 },
{ user_id : '57c7f4312b3c9771219bd21c', totalprice : 533 },
{ user_id : '57c7f4312b3c9771219bd21c', totalprice : 544 },
{ user_id : '57efb93fdc78c816a3a15c4a', totalprice : 250 },
{ user_id : '57efb93fdc78c816a3a15c4a', totalprice : 157 },
{ user_id : '57efb93fdc78c816a3a15c4a', totalprice : 149 },
{ user_id : '57efb93fdc78c816a3a15c4a', totalprice : 104 }
我期待的输出象下面这样:
{
user_id : '57c7f4312b3c9771219bd21c',
finalTotal : 1262
},
{
user_id : '57efb93fdc78c816a3a15c4a'
finalTotal : 660
}
OK,让我补充表数据。 –