并按汇总汇总每周

并按汇总汇总每周

本文介绍了猫鼬的日期,没有时间进行比较,并且按createdAt和staffId进行分组,并按汇总汇总每周,每月和每年的工作人员总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的问题是重复的,但对此表示歉意.

Might be question is look like duplicate but apologize for it.

我想汇总 createdAt 属性的每周,每月,每年结果基础,而没有时间和 staffId .

I want to aggregate the weekly, Monthly, Yearly result basis of createdAt property without time and staffId.

模型如下:

   {
    "_id" : ObjectId("5f351f3d9d90b1281c44c5dp"),
    "staffId" : 12345,
    "category" : "trend",
    "page_route" : "http://example.com/rer",
    "expireAt" : ISODate("2020-08-13T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f351f3d9d90b1281c44c5de"),
    "staffId" : 12346,
    "category" : "incident",
    "page_route" : "http://example.com/rergfhfhf",
    "expireAt" : ISODate("2020-08-14T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-08T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-12T11:08:45.199Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f351f3d9d90b1281c44c5dc"),
    "staffId" : 12347,
    "category" : "trend",
    "page_route" : "http://example.com/rerrwe",
    "expireAt" : ISODate("2020-08-13T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-13T11:08:45.199Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f351f3d9d90b1281c44c5dr"),
    "staffId" : 12348,
    "category" : "trend",
    "page_route" : "http://example.com/rerrwe",
    "expireAt" : ISODate("2020-08-12T11:08:45.196Z"),
    "createdAt" : ISODate("2020-08-08T11:08:45.199Z"),
    "updatedAt" : ISODate("2020-08-12T11:08:45.199Z"),
    "__v" : 0
}

预期结果:示例1)每周

[
{_id: "2020-11-13", total: 2},
{_id: "2020-11-8", total: 2},


]

示例2)每月

[
{_id: "2020-11-8", total: 4},

]

类似地每年...

我正在使用 nodejs 猫鼬来实现 API .

我很努力,但我无法达到预期的结果.

I am struggle lot but I am unable to achieve the expected result.

如果有人帮助我,那将是很大的帮助.

if anyone help me then it will be great help.

感谢所有专家.

我尝试过这样的事情:

[
        {
          $match: {
            createdAt: { $gte: new Date(currentDate), $lt: new Date(nextDate) }
          }
        },
        {
          $project: {
            _id: 1,
            staffId: 1,
            day: {
              $dayOfMonth: "$createdAt"
            },
            month: {
              $month: "$createdAt"
            },
            year: {
              $year: "$createdAt"
            }
          }
        },
        {
          $project: {
            _id: 1,
            staffId: 1,
            datetime: {
              $concat: [
                {
                  $substr: ["$year", 0, 4]
                },
                "-",
                {
                  $substr: ["$month", 0, 2]
                },
                "-",
                {
                  $substr: ["$day", 0, 2]
                }
              ]
            }
          }
        },
        {
          $group: {
            _id: {
              createdAt: "$datetime",
              staffId: "$staffId"
            }
          }
        },
        {
          $group: {
            _id: {$week:"$_id.createdAt"},
            total: {
              $sum: 1
            }
          }
        },
        { $sort: { _id: 1 } }
      ];

推荐答案

您可以尝试

  • 按周分组
db.collection.aggregate([
  {
    $group: {
      _id: {
        year: { $year: "$createdAt" },
        week: { $week: "$createdAt" }
      },
      createdAt: { $first: "$createdAt" },
      count: { $sum: 1 }
    }
  }
])

游乐场

  • 按月分组
db.collection.aggregate([
  {
    $group: {
      _id: {
        year: { $year: "$createdAt" },
        month: { $month: "$createdAt" }
      },
      createdAt: { $first: "$createdAt" },
      count: { $sum: 1 }
    }
  }
])

游乐场

  • 按年份分组
db.collection.aggregate([
  {
    $group: {
      _id: { $year: "$createdAt" },
      createdAt: { $first: "$createdAt" },
      count: { $sum: 1 }
    }
  }
])

游乐场

这篇关于猫鼬的日期,没有时间进行比较,并且按createdAt和staffId进行分组,并按汇总汇总每周,每月和每年的工作人员总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:00