我想使用聚合和分组阶段对所有“总计”字段值求和。我的代码和文件在这里
单据
{
"_id": "57a068477b2l51ec16eb7das",
"userID":"5742c6eedsaadsd93573e",
"profileID":"5742aee49adv520593573c",
"date": 1470130247779,
"updateDate": 1470130361342,
"total": 2
}
{
"_id": "57a068477b2l51ec16eb983",
"userID":"5742c6eedsaadsd93573e",
"profileID":"5742aee49adv520593573c",
"date": 1470130247779,
"updateDate": 1470130361342,
"total": 1
}
我在下面查询“总”值的总和
RecallProfile.aggregate([
{
"$match": {
"updateDate": {
"$gte": from, "$lte": to
}
}
},
{
$group: {
"_id": "$updateDate",
"totalRecord": {"$sum": "$total"}
}
}
], function (err, result) {
if (err) {
console.log('error ', err);
res.sendStatus(500);
} else {
console.log("result", result);
res.sendStatus(200);
}
})
其给出“结果[]”日志。
我尝试过,它正在工作。似乎问题是往返。在聚合$ match管道中,我们需要与字段具有相同的类型。parseInt()
当我们从快速请求查询中获取字符串形式时,您需要使用变量。
"$match": {
"updateDate": {
"$gte": parseInt(from), "$lte": parseInt(to)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句