猫鼬日期比较没有时间和按createdAt和staffId分组,每周、每月和每年按聚合计算的员工总数?
Posted
技术标签:
【中文标题】猫鼬日期比较没有时间和按createdAt和staffId分组,每周、每月和每年按聚合计算的员工总数?【英文标题】:mongoose Date comparing without time and Group by createdAt and staffId with Weekly, monthly and Yearly total of staff count by aggregation? 【发布时间】:2021-03-24 21:02:44 【问题描述】:可能问题看起来像重复但为此道歉。
我想汇总createdAt
属性无时间和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 和 mongoose 来实现 API。
我很挣扎,但我无法达到预期的结果。
如果有人帮助我,那将是很大的帮助。
感谢所有专家。
我尝试过这样的事情:
[
$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
];
【问题讨论】:
到目前为止你尝试了什么? @WernfriedDomscheit ,我已经更新了相关问题。可以请指导 【参考方案1】:你可以试试,
按周分组db.collection.aggregate([
$group:
_id:
year: $year: "$createdAt" ,
week: $week: "$createdAt"
,
createdAt: $first: "$createdAt" ,
count: $sum: 1
])
Playground
按月分组db.collection.aggregate([
$group:
_id:
year: $year: "$createdAt" ,
month: $month: "$createdAt"
,
createdAt: $first: "$createdAt" ,
count: $sum: 1
])
Playground
按年份分组db.collection.aggregate([
$group:
_id: $year: "$createdAt" ,
createdAt: $first: "$createdAt" ,
count: $sum: 1
])
Playground
您可以使用
createdAt
字段从客户端语言更改日期格式!
【讨论】:
有一个小问题,如果相同的 staffid 在同一日期多次出现,那么它应该返回 1 个计数,因为我们希望在同一时间与 createdAt 和 staffId 进行分组。我们如何实现这一目标 您可以按staffId分组,也可以像playground这样,如果这不是您的期望,请提供预期的结果。以上是关于猫鼬日期比较没有时间和按createdAt和staffId分组,每周、每月和每年按聚合计算的员工总数?的主要内容,如果未能解决你的问题,请参考以下文章