0

I have a records collection with thousands of documents, like the following:

{ "_id" : ObjectId("5ee1e209e07f053f990cea8c"), "key" : "TAKwGc6Jr4i8Z487", "createdAt" : ISODate("2017-01-28T01:22:14.398Z"), "counts" : [ 150, 160 ], "value" : "Doc1" }
{ "_id" : ObjectId("5ee1e8dee07f053f990ceaa1"), "key" : "TAKwGc6Jr4i8Z487", "createdAt" : ISODate("2017-01-28T01:22:14.398Z"), "counts" : [ 170 ], "value" : "Doc2" }
...

I want to write a query to select them by date range, and by the total count of elements in the counts array field.

I haven't managed to find any resources on that. Should an aggregation be used, in this case?

Like $sum something?

{$and:[
  {createdAt:{$gte:ISODate('2016-01-30'),$lt:ISODate('2016-02-01')}},
  {<sum> ???}
]}

1 Answer 1

1

You can add a field to calculate sum of counts using $addFields, then use $match to compare sum and finally remove that temporary field before return using $unset.
Here is the playground for your example.

Sign up to request clarification or add additional context in comments.

2 Comments

Please, how can I add the range date filter to your query? Also, can I use $gt and $lte instead of $match ?
I've modified the playground link and added filter by date range for createdAt. You can use $gt and $lte in $match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.