如何按月、周和日汇总 - MongoDB

How to aggregate by Month, Week and Day - MongoDB

我已经收集了门票,我需要获取当天、每周和每月售出的门票数量。下面是结构

{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 100,
    "createdAt": "2021-11-08T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 101,
    "createdAt": "2021-11-07T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 102,
    "createdAt": "2021-11-06T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 103,
    "createdAt": "2021-11-05T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 104,
    "createdAt": "2021-11-04T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 105,
    "createdAt": "2021-11-03T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 106,
    "createdAt": "2021-11-02T08:34:39.697+00:00"
},
{
    "_id": "9917aed79fcc274bdefgj",
    "ticketId": 107,
    "createdAt": "2021-11-01T08:34:39.697+00:00"
}

我是 MongoDB 的新手,我尝试使用项目或匹配来导出日、周或月,但我没有成功,因此无法分享我的大部分工作。感谢任何帮助

更新:
查询 11 月 8 日时的预期输出
天:1
周:1
月份:8

输出解释:
第 1 天 — 没有。在给定日期售出的门票数量
周:1 - 没有。指定周售出的门票数量(从星期一开始,仅售出一张)
月:8 — 没有。在给定月份售出的门票中,售出了 8 张门票

Test Here

db.collection.aggregate([
  {
    "$project": {
      "createdAtWeek": {
        "$week": "$createdAt"
      },
      "createdAtMonth": {
        "$month": "$createdAt"
      },
      "createdAtDay": {
        "$dayOfMonth": "$createdAt"
      }
    }
  },
  {
    "$group": {
      "_id": {
        createdAtWeek: "$createdAtWeek",
        createdAtMonth: "$createdAtMonth",
        createdAtDay: "$createdAtDay"
      },
      count: {
        $sum: 1
      }
    }
  }
])

查询

  • 将您想要的日期放在第一组中,如果您想要当前日期,请保留具有当前日期的 $$NOW 系统变量
  • year/month/week/day
  • 的 4 倍相同代码
  • 此处在组之前进行过滤,以与 $$NOW 日期(即当前日期)具有相同的年、月、周或日。

*我使用 $isoWeek 表示每周从星期一开始 $week 每周从星期日开始,您可以更改它们

PlayMongo

aggregate(
[{"$set": {"date": "$$NOW"}},
  {"$facet": 
    {"year": 
      [{"$match": 
          {"$expr": {"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]}}},
        {"$group": {"_id": null, "count": {"$sum": 1}}},
        {"$unset": ["_id"]}],
      "month": 
      [{"$match": 
          {"$expr": 
            {"$and": 
              [{"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]},
                {"$eq": [{"$month": "$date"}, {"$month": "$createdAt"}]}]}}},
        {"$group": {"_id": null, "count": {"$sum": 1}}},
        {"$unset": ["_id"]}],
      "week": 
      [{"$match": 
         {"$expr": 
           {"$and": 
            [{"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]},
             {"$eq": [{"$isoWeek": "$date"}, {"$isoWeek": "$createdAt"}]}]}}},
        {"$group": {"_id": null, "count": {"$sum": 1}}},
        {"$unset": ["_id"]}],
      "day": 
      [{"$match": 
          {"$expr": 
            {"$and": 
              [{"$eq": [{"$year": "$date"}, {"$year": "$createdAt"}]},
                {"$eq": 
                  [{"$dayOfYear": "$date"}, {"$dayOfYear": "$createdAt"}]}]}}},
        {"$group": {"_id": null, "count": {"$sum": 1}}},
        {"$unset": ["_id"]}]}},
  {"$set": 
    {"year": 
      {"$cond": 
        [{"$eq": ["$year", []]}, 0, {"$arrayElemAt": ["$year.count", 0]}]},
      "month": 
      {"$cond": 
        [{"$eq": ["$month", []]}, 0, {"$arrayElemAt": ["$month.count", 0]}]},
      "week": 
      {"$cond": 
      [{"$eq": ["$week", []]}, 0, {"$arrayElemAt": ["$week.count", 0]}]},
      "day": 
      {"$cond": 
        [{"$eq": ["$day", []]}, 0, {"$arrayElemAt": ["$day.count", 0]}]}}}])

结果
(“日期”为 11 月 9 日,我得到的数据)

[
  {
    "day": 1,
    "month": 8,
    "week": 1,
    "year": 8
  }
]