I have documents similar to this:
{_ id: 1, values: [2,3,4]}
{_ id: 2, values: [4]}
{_ id: 3, values: [3,4,5,6,7,8,9,10,11]}
in which each doc has an array. I need a query that returns a document only if the EACH element of its array meets the required criteria (and does not match ANY).
Eg. something like (but not)
{'values': {'$ gt': 1, '$ lt': 5}})
which will successfully return the first two, but not the third document, since not all elements of the third value of the doc array meet the criteria.
Apparently mongodb uses an implicit OR in requests for arrays, whereas I need AND.
I think I could manually index each element, for example:
collection.find ({values.0: {$ gt: 1, $ lt: 5}, values.1: {$ gt: 1, $ lt: 5}, ... values.n: {$ gt: 1, $ lt: 5}}), but this is a pain with my dynamic arrays.
Is there a better way?
Note. I asked a mongodb user about this, but a newbie to mongodb created confusion with $ all operator. Here I am concerned about the doc array, not the array of requests. Also, in this numerical case, I understand that you can write a query that negates the desired range, but in general I will not write negation.