2

我想使用汇总和小组阶段将所有“总计”字段值相加。 我的代码和文档是在这里

文件

{ 
    "_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); 
     } 
    }) 

它给 “导致[]” 日志。

回答

0

我尝试过,它工作。看起来这个问题与来自并来自于。在聚合$匹配管道中,我们需要与field有相同的类型。您需要parseInt()这些变量,因为我们获得了字符串表单的请求查询。

"$match": { 
    "updateDate": { 
     "$gte": parseInt(from), "$lte": parseInt(to) 
    } 
}