Podes tentar,
- Agrupar por semana
db.collection.aggregate([
{
$group: {
_id: {
year: { $year: "$createdAt" },
week: { $week: "$createdAt" }
},
createdAt: { $first: "$createdAt" },
count: { $sum: 1 }
}
}
])
Playground
- Agrupar por mês
db.collection.aggregate([
{
$group: {
_id: {
year: { $year: "$createdAt" },
month: { $month: "$createdAt" }
},
createdAt: { $first: "$createdAt" },
count: { $sum: 1 }
}
}
])
Playground
- Agrupar por ano
db.collection.aggregate([
{
$group: {
_id: { $year: "$createdAt" },
createdAt: { $first: "$createdAt" },
count: { $sum: 1 }
}
}
])
Playground