使用mongoose.js按月和年分组 [英] groups by month and year using mongoose.js

查看:444
本文介绍了使用mongoose.js按月和年分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中的收藏如下:

my collection in mongodb looks like below:

{
    "AccountID" : "87f7fd60-d1ad-11e2-98bb-795730bce125",
    "userId" : ObjectId("51b59fbec46916e60d00000c"),
    "_id" : ObjectId("51b6e603e3efef161b000003"),
    "accessDate" : ISODate("2013-06-11T08:55:31.957Z"),
    "__v" : 0
}
{
        "AccountID" : "47f7fd60-d1ad-11e2-98bb-795730bce125",
        "userId" : ObjectId("51b59fbec46916e60d00000d"),
        "_id" : ObjectId("51b6e603e3efef161b000003"),
        "accessDate" : ISODate("2013-05-1T08:05:31.957Z"),
        "__v" : 0
}

i我要写什么查询结果如下: 这是按月和年以及每天的计数分组的结果.

i what to write a query which results the below result: this is result as grouped by month and year and the count per day.

{
  "usage": [
    {
      "year": 2013,
      "monthlyusage": [
        {
          "month": 1,
          "dailyusage": [
            {
              "day": 1,
              "count": 205
            },
            {
              "day": 2,
              "count": 1109
            },
            {
              "day": 4,
              "count": 455
            }
          ]
        },
        {
          "month": 2,
          "dailyusage": [
            {
              "day": 11,
              "count": 256
            },
            {
              "day": 2,
              "count": 1001
            },
            {
              "day": 5,
              "count": 65
            }
          ]
        }
      ]
    },
    {
      "year": 2012,
      "monthlyusage": [
        {
          "month": 12,
          "dailyusage": [
            {
              "day": 1,
              "count": 78
            },
            {
              "day": 2,
              "count": 7009
            },
            {
              "day": 28,
              "count": 55
            }
          ]
        },
        {
          "month": 11,
          "dailyusage": [
            {
              "day": 11,
              "count": 800
            },
            {
              "day": 2,
              "count": 5094
            },
            {
              "day": 25,
              "count": 165
            }
          ]
        }
      ]
    }
  ]
}

我如何使用mongoose.js框架

How can i do this using mongoose.js framework

推荐答案

Mongoose为MongoDB聚合框架提供了一个轻量级的包装器.如果您不熟悉汇总,可以从MongoDB文档中了解更多信息: http://docs.mongodb.org/手动/汇总/

Mongoose provides a lightweight wrapper around the MongoDB aggregation framework. If you're new to aggregation, you can learn more about in from the MongoDB docs: http://docs.mongodb.org/manual/aggregation/

要将数据整理成上述形式,可以使用带有一系列$ group操作的聚合管道.这里使用的是猫鼬框架:

To massage your data into the form you've described above, you can use an aggregation pipeline with a series of $group operations. Here it is using the mongoose framework:

var dateSchema = mongoose.Schema({…});
var DateItem = mongoose.model('DateItem', dateSchema);

DateItem.aggregate(
      { $group : { 
           _id : { year: { $year : "$accessDate" }, month: { $month : "$accessDate" },day: { $dayOfMonth : "$accessDate" }}, 
           count : { $sum : 1 }}
           }, 
      { $group : { 
           _id : { year: "$_id.year", month: "$_id.month" }, 
           dailyusage: { $push: { day: "$_id.day", count: "$count" }}}
           }, 
      { $group : { 
           _id : { year: "$_id.year" }, 
           monthlyusage: { $push: { month: "$_id.month", dailyusage: "$dailyusage" }}}
           }, 
      function (err, res)
           { if (err) ; // TODO handle error 
             console.log(res); 
           });
});

第一个$ group将产生这种形式的文档,每天一个:

The first $group will result in documents of this form, one for each day:

{ 
  "_id" : { "year" : 2013, "month" : 8, "day" : 15 },
  "count" : 1
}

第二个$ group将导致按月分组的文档:

The second $group will result in documents grouped by month:

{
  "_id" : { "year" : 2012, "month" : 11 },
 "dailyusage" : [
          { "day" : 6, "count" : 1 },
          { "day" : 9, "count" : 1 },
          ... ]
},

第三个$ group将产生更大的文档,每年一个.

And the third $group will result in even larger documents, one for each year.

此查询会将您的数据聚合到大型的分层文档中.但是,如果您打算在汇总后对该数据运行查询,那么这可能不是最有用的数据形式.请考虑如何使用汇总数据.包含更多较小文档的架构(也许每月一个甚至每天一个)可能更方便.

This query will aggregate your data into large, hierarchical documents. If you plan to run queries on this data after aggregation, however, this might not be the most useful form for your data to be in. Consider how you'll be using the aggregated data. A schema involving more smaller documents, perhaps one per month or even one per day, might be more convenient.

这篇关于使用mongoose.js按月和年分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆