I have the following documents in my MongoDB-database:
{"stats": [2,5,1]}
{"stats": [1,9,2]}
Now I want to find those documents which have a stats-array that contains the values 2 and 1 (in that order). So only the first document should be found. A simple $all-query would return both documents. Is there a way to do this with MongoDB’s query-language?
Or lets assume the following documents:
{"stats": [{"val": 2, "position": 0},{"val": 5, "position": 1},{"val": 1, "position": 2}]}
{"stats": [{"val": 1, "position": 0},{"val": 9, "position": 1},{"val": 2, "position": 2}]}
Same question 🙂 Is there a way to query these documents to return only those which have a value 2 followed by a value 1 (take the position-value into account)?
I could probably use a $where-query, but the documentation says that this is not the preferred way (and is slow) 🙂
$all and $in do not take any ordering into account. Means: not doable with the standard query syntax.