【问题标题】:Mongoose aggregate over date rangeMongoose 在日期范围内聚合
【发布时间】:2014-01-29 03:19:58
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    此语法不正确:

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

    $count 不是上面的操作符,只是一个字段引用。 $group 中不能有“裸”字段引用。您需要使用聚合运算符,例如:

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

    请展示一些示例文档,我可以更详细地更新答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多