关于node.js:使用$ group阶段和$ sum运算符进行聚合

aggregation with $group stage and $sum operator

我想使用聚合和分组阶段对所有"总计"字段值求和。
我的代码和文档在这里

文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "_id":"57a068477b2l51ec16eb7das",
   "userID":"5742c6eedsaadsd93573e",
   "profileID":"5742aee49adv520593573c",
   "date": 1470130247779,
   "updateDate": 1470130361342,
   "total": 2
}
{
   "_id":"57a068477b2l51ec16eb983",
   "userID":"5742c6eedsaadsd93573e",
   "profileID":"5742aee49adv520593573c",
   "date": 1470130247779,
   "updateDate": 1470130361342,
   "total": 1
}

我正在执行以下查询以获取"总计"值的总和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    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()变量。

1
2
3
4
5
"$match": {
   "updateDate": {
       "$gte": parseInt(from),"$lte": parseInt(to)
    }
}