mongoose聚集date范围
我正在尝试mongoose聚集。 我想获得date范围内的字段的总和。 http://mongoosejs.com/docs/api.html#model_Model.aggregate
mymodel.aggregate({$match:{date:{$gte: fromDate, $lt: toDate}}}, {$project:{_id:0,date:1, count:1}}, {$group:{'_id':0,count:"$count"}}).exec(function(err,data){ if(err){ console.log('Error Fetching model'); console.log(err); } callback(false, data); });
这不起作用。
{ [MongoError: exception: the group aggregate field 'count' must be defined as an expression inside an object] name: 'MongoError', errmsg: 'exception: the group aggregate field \'count\' must be defined as an expression inside an object', code: 15951, ok: 0 }
任何想法?
这个语法不正确:
{$group:{'_id':0,count:"$count"}}
$count
不是上面的操作符,而只是一个字段引用。 $group
不能有“裸体”字段引用。 您需要使用聚合运算符,如下所示:
{$group:{'_id':0,count:{"$sum": "$count"}}}
请出示一些示例文档,我可以更详细地更新答案。