2014-01-10 92 views
1

我正在尝试猫鼬聚集。我想获得日期范围内的字段总数。 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 } 

任何想法?

回答

2

这个语法是不正确的:

{$group:{'_id':0,count:"$count"}} 

$count是不是在上述运营商,而只是一个字段引用。您不能在$group中拥有“裸体”字段参考。您需要使用聚合操作,就像在:

{$group:{'_id':0,count:{"$sum": "$count"}}} 

请出示了一些简单的文件,我可以在一个更复杂的方式更新了答案。

+0

谢谢:)它工作:) – Neo