java MongoDB 聚合查询aggregation

通过aggregation进行分组查询,有时候需要统计查询记录总条数假设分组字段为day。如果查询函数写为:

{
     $group: {
        _id: "day",
        count: { $sum: 1 }
     }
   }

那么查询出来的数量为记录条数,结果如图所示:



如果查询函数写为:

{
     $group: {
        _id: "$day",
        count: { $sum: 1 }
     }
   }
查询结果中的count为当前分组的条目数,不是数据总数。如图所示: