Можете да опитате,
- Групиране по седмица
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 }
}
}
])