Home:ALL Converter>mongodb query to match each element in doc array to a condition

mongodb query to match each element in doc array to a condition

Ask Time:2011-05-18T09:04:38         Author:ricopan

Json Formatter

I have docs analogous 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 only returns the doc if EACH element of its array match the desired criteria (rather than if ANY element matches).

Eg. something like (but not)

{ 'values' : { '$gt' : 1, '$lt': 5} })

which would successfully return the first two but not the third doc, as not all elements of the third doc's array 'values' match the criteria.

Apparently mongodb uses an implicit OR in queries on arrays, whereas I need AND.

I guess I could manually index each element, eg.:

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 highly dynamic arrays.

Is there a better way?

Note: I asked this over at mongodb-user but being new to mongodb generated confusion with $all operator. Here I am concerned about the doc array, not a query array. Also, in this numeric case I realize one might write a query that negates the range desired, but in general I won't be able to write the negation.

Author:ricopan,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6038818/mongodb-query-to-match-each-element-in-doc-array-to-a-condition
Chris Fulstow :

I don't think there's any way to do this yet, apart from manually iterating through your documents and checking each value in the array. That's going to be quite slow because it has to execute JavaScript on each document, and can't take advantage of any index over col.values.\n\nEven a $where JavaScript expression query doesn't seem work here because, possibly because the query contains a callback and is too complex:\n\ndb.col.find(\"this.values.every(function(v) { return (v > 1 && v < 5) })\")\n\n\n\n\nEdit: For some queries, including this one, the JavaScript $where expression needs a return statement, so this works fine:\n\ndb.col.find(\"return this.values.every(function(v) { return (v > 1 && v < 5) })\")\n",
2011-05-18T02:09:57
Tobia :

MongoDB, like most (if not all) databases, only implements the existential quantificator (∃, exists) and its negation (∄, does not exist.) It does not have the universal quantificator (∀, for all) nor its negation, because they cannot be optimized using indices, and therefore wouldn't be useful in practice.\n\nFortunately, in first-order logic, every statement involving ∀ can be transformed in an equivalent statement involving ∃.\n\nIn your example, the statement:\n\n\n ∀ x ∈ values : x > 1 ∧ x < 5 \n\n\nor \"all values are > 1 and < 5\" is equivalent to \n\n\n ∄ x ∈ values : ¬(x > 1 ∧ x < 5) \n\n\nor \"there is no value that is not > 1 and < 5\" which by De Morgan's laws becomes:\n\n\n ∄ x ∈ values : x ≤ 1 ∨ x ≥ 5\n\n\nor \"there is no value that is ≤ 1 or ≥ 5\"\n\nThe latter can be expressed in MongoDB in many ways, for example:\n\n> db.test.remove()\n> db.test.insert({_id: 1, values: [2, 3, 4]})\n> db.test.insert({_id: 2, values: [4]})\n> db.test.insert({_id: 3, values: [3, 4, 5, 6, 7, 8, 9, 10, 11]})\n\n> db.test.find({$nor: [{values: {$lte: 1}}, {values: {$gte: 5}}]})\n{ \"_id\" : 1, \"values\" : [ 2, 3, 4 ] }\n{ \"_id\" : 2, \"values\" : [ 4 ] }\n",
2015-02-02T15:21:49
John Flatness :

You'll sacrifice speed, but you could use a JavaScript expression, either passed directly to find() or to $where.\n\nYou'd be able to loop through the elements in your array and only return true if all of them satisfied your condition.",
2011-05-18T01:38:13
user2665694 :

The thread \n\nhttp://groups.google.com/group/mongodb-user/browse_thread/thread/dad19e28c1acbd49\n\nexplains your options.\n\nAs 'chx' wrote: it is not possible. You now know the functionality of the $all and $or operators. That's all you have and get from MongoDB - nothing more, nothing less.",
2011-05-18T04:15:49
yy