#mongoose bucket (min accumulator)

15 messages · Page 1 of 1 (latest)

warm mica
#

@ruby zodiac Could you please share your collection schema information ?

ruby zodiac
#

I am applying this aggregation to all models so there is nothing special about it

warm mica
#

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 ?

ruby zodiac
#

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

ruby zodiac
warm mica
#

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 ?

ruby zodiac
#

Yes,
So for boundaries of
2024-06
2024-07

there is 1 record containing min,max for example

warm mica
#

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

#
  1. it would first find the records in the range
  2. group by the date only
  3. count the records
  4. sort would give you the minimum
  5. limit will give you the most minimum
ruby zodiac
#

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

warm mica
#

i did not get that point. could you please explain a bit more on that ?

#

I have not used $bucket stage. I just googled it.

#

But I think it's achievable with $bucket as well.