【问题标题】:MongoDB Month wise groupingMongoDB 按月分组
【发布时间】:2020-06-04 17:26:16
【问题描述】:

有人可以帮忙获取我给定数据的最近 6 个月的计数吗?

这是我的数据

  {
    "_id" : ObjectId("59815d4704ca1760a45957ca"),
    "ticketId" :"TCKT0HF652Y",
    "createdAt" : ISODate("2020-06-02T05:03:57Z")
},
{
    "_id" : ObjectId("59815d5404ca1760a45957cb"),
    "ticketId" :"TCKT0HF8849A",
    "createdAt" : ISODate("2020-06-02T05:04:11Z")
},
{
    "_id" : ObjectId("5980191d04ca1760a45957cd"),
    "ticketId" :"TCKT0H4953Z",
    "createdAt" : ISODate("2020-05-01T06:00:46Z")
},
  {
    "_id" : ObjectId("59815d4704ca1760a45957ca"),
    "ticketId" :"TCKT0HF339Y",
    "createdAt" : ISODate("2020-05-02T05:03:57Z")
},
{
    "_id" : ObjectId("59815d5404ca1760a45957cb"),
    "ticketId" :"TCKT0HF839A",
    "createdAt" : ISODate("2020-05-02T05:04:11Z")
},
{
    "_id" : ObjectId("5980191d04ca1760a45957cd"),
    "ticketId" :"TCKT0HF9582Z",
    "createdAt" : ISODate("2020-04-01T06:00:46Z")
}

我的查询是

Tickets.aggregate([
          {
            $project: {
              count: {$sum: 1},
              month: {$month: "$createdAt"},
              year: {$year: "$createdAt"},
            },
          },
          {
            $group: {
              _id: {month: "$month", year: "$year"},
              total: {$sum: "$count"},
            },
          },
        ])

这就是我得到的结果

"data": [
            {
                "_id": {
                    "month": 6,
                    "year": 2020
                },
                "total": 2
            },
            {
                "_id": {
                    "month": 5,
                    "year": 2020
                },
                "total": 3
            },
            {
                "_id": {
                    "month": 4,
                    "year": 2020
                },
                "total": 1
            }
        ]

我的要求是生成最近 6 个月的数据,无论该月是否有数据。所以我期待的结果是

"data": [
            {
                "_id": {
                    "month": 6,
                    "year": 2020
                },
                "total": 2
            },
            {
                "_id": {
                    "month": 5,
                    "year": 2020
                },
                "total": 3
            },
            {
                "_id": {
                    "month": 4,
                    "year": 2020
                },
                "total": 1
            },
           {
              "_id": {
                    "month": 3,
                    "year": 2020
                },
                "total": 0
            },
           {
               "_id": {
                    "month": 2,
                    "year": 2020
                },
                "total": 0
           },
           {
               "_id": {
                    "month": 1,
                    "year": 2020
                },
                "total": 0
           }
        ]

我们还可以显示月份名称而不是月份数字吗?

【问题讨论】:

  • 关于采取6个月时间窗口的部分,您可以在管道中添加$match作为分组前过滤数据的第一阶段`` { $match:{ createdAt: { $gt: startTs, $lt: endTs, }, } } ``` 将月份显示为字符串的部分,您可以再次使用 $project 并以不同方式显示。
  • 那么那些没有数据但我仍想显示月份名称并计数为 0 的月份呢?我该如何实现呢?
  • 说实话我不知道,但也许如果你为 mongo 添加更多标签,它可以帮助观看 mongo 标签的人回答你的问题

标签: javascript node.js mongodb mongoose mongodb-query


【解决方案1】:

我认为您不会在 MongoDB 中本地输出月份名称。我建议Moment.js 图书馆。可能是这个:

Tickets.aggregate([
   {
      $group: {
         _id: {
            month: { $month: "$createdAt" },
            year: { $year: "$createdAt" }
         },
         total: { $sum: 1 },
      },
   }
]).forEach(function (row) {
   print(moment.utc(row._id.year + "-" + row._id.month, "YYYY-MM").format("MMMM YYYY") + ": " + row.total);
})

【讨论】:

    猜你喜欢
    • 2023-02-13
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2015-03-06
    相关资源
    最近更新 更多