#mongoose bucket (min accumulator)
15 messages · Page 1 of 1 (latest)
Consider it normal schema with createdAt field on it
I am applying this aggregation to all models so there is nothing special about it
ModelName.count({your_query}).gte({date}).lte({date});
Won't this work ?
I have to check the syntax but this is what you want right ? count of docs between a date range ? correct ?
No, I want the count of the minimum day docs inserted
E.g:
2024-06-09 there is 10 docs with this createdAt Date
2024-06-10 there is 2 docs with this createdAt Date
So the minimum here is 2
Did you understand my goal 😅
I see, let me think.
even if i search within a range, let's say 1st of the month to the last day of the month, in output: I shall only get 1 RECORD count for any one day that has the least amount of records inserted, correct ?
Yes,
So for boundaries of
2024-06
2024-07
there is 1 record containing min,max for example
ModelName.aggregate([
{
$match: {
createdAt: {
$gte: new Date(startDate),
$lte: new Date(endDate),
},
},
},
{
$group: {
_id: {
$dateToString: { format: "%Y-%m-%d", date: "$createdAt" },
},
count: { $sum: 1 },
},
},
{
$sort: { count: 1 },
},
{
$limit: 1,
},
]
Try this one @ruby zodiac
- it would first find the records in the range
- group by the date only
- count the records
- sort would give you the minimum
- limit will give you the most minimum
But I want to use it along with the Bucket so I can apply it for all periods (bucket boundaries) not only for two dates