问题
Might be question is look like duplicate but apologize for it.
I want to aggregate the weekly, Monthly, Yearly result basis of createdAt
property without time and staffId
.
Model is like below:
{
"_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
}
Expected result: Example 1) Weekly
[
{_id: "2020-11-13", total: 2},
{_id: "2020-11-8", total: 2},
]
Example 2) Monthly
[
{_id: "2020-11-8", total: 4},
]
similarly for yearly ...
I am using nodejs and mongoose to implement the API.
I am struggle lot but I am unable to achieve the expected result.
if anyone help me then it will be great help.
Thanks to all Expert.
I have tried something like this:
[
{
$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 } }
];
回答1:
You can try,
- Group by week
db.collection.aggregate([
{
$group: {
_id: {
year: { $year: "$createdAt" },
week: { $week: "$createdAt" }
},
createdAt: { $first: "$createdAt" },
count: { $sum: 1 }
}
}
])
Playground
- Group by month
db.collection.aggregate([
{
$group: {
_id: {
year: { $year: "$createdAt" },
month: { $month: "$createdAt" }
},
createdAt: { $first: "$createdAt" },
count: { $sum: 1 }
}
}
])
Playground
- Group by year
db.collection.aggregate([
{
$group: {
_id: { $year: "$createdAt" },
createdAt: { $first: "$createdAt" },
count: { $sum: 1 }
}
}
])
Playground
You can change date format from your client side language using
createdAt
field!
来源:https://stackoverflow.com/questions/65285364/mongoose-date-comparing-without-time-and-group-by-createdat-and-staffid-with-wee