Given the following document structure:
{
_id : Mongo ID,
data : [someData1, someData2, ..., someDataN]
}
I want to get all documents which data
size is between a
and b
(strict positive integers, consider a < b
is always true).
I first thought about using $size
, but the Mongo doc states:
$size
does not accept ranges of values. To select documents based on fields with different numbers of elements, create a counter field that you increment when you add elements to a field.
(Emphasis mine)
How to retrieve all the documents with a data
array of length between a
and b
without using a counter?
Related but did not solve:
- This answer which suggests using a counter
- This question which asks how to query an internal length and got many answers
- This answer which suggests to use:
{ 'data.' + b : { $exists : true } }
but I don't know how reliable it is and how it could apply to a range - This question about how to find documents with a minimal array length