简体   繁体   English

如何获得猫鼬分组数据的总和

[英]How can I get sum of grouped data with mongoose

I have a collection includes log data. 我有一个包含日志数据的集合。

[
   {
      "logType":1,
      "created_at": 2015-12-15 07:38:54.766Z
   },
   ..
   .. 
   ..,
   {
      "logType":2,
      "created_at": 2015-13-15 07:38:54.766Z
   }
]

I want group by created_at field and get group element counts. 我想要由created_at字段分组并获取分组元素计数。

My goal is show infographic which includes total logs count by weekly. 我的目标是显示信息图表,其中包括每周的总日志数。

You can group by week using $week and $year : 您可以使用$ week$ year按周分组:

db.logs.aggregate([
    {
      $group:{
         _id: {
           year: { $year:"$created_at" },
           week: { $week:"$created_at" }
         },
         count: { $sum:1 }
      }
    }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM