Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo db findOne and $or does order of arguments matters or hierarchy? [performance]

I mean for example while having two conditions: if first condition true will it avoid checking the second?

doc = collection.find_one(
    {'$or': [
              {
               'k': kind,
               'i': int(pk)
              },
              {
               'children.k': kind, 
               'children.i': int(pk)
              }

            ]
    }, { '_id': False})

I would like that it stops further searching when matching first condition, so it not goes lower level to search around children.

Is it matter of arguments order in $OR closure or rather mongodb knows smartly the hierarchy and it influences order of search by findOne?

like image 794
andilabs Avatar asked Aug 28 '14 08:08

andilabs


People also ask

Does order of query matter MongoDB?

Generally, the order of query operators doesn't matter...

Which of the following is incorrect statement about Find and findOne operations in MongoDB?

Q 7 - Which of the following is incorrect statement about find and findOne operations in MongoDB? A - find() returns all the documents in a collection while findOne() retrieves only the first one.

How does findOne work in MongoDB?

MongoDB findOne() method returns only one document that satisfies the criteria entered. If the criteria entered matches for more than one document, the method returns only one document according to natural ordering, which reflects the order in which the documents are stored in the database.

Is findOne deprecated?

It looks like findOne() is only deprecated in the Javascript driver.


2 Answers

I would like that it stops further searching when matchin first condition, so it not goes lower level to search around children.

The question you have to ask yourself is: How can MongoDB know how both sides of the $or are satisfied by one side? How does MongoDB know that the documents that do not satisfy the first condition do not satisfy the second?

If I were to say that I have a set of documents, half with {a:1,b:1} and half with {b:2} how can you know that a:1 OR b:1 is satisfied by the first half if you have no idea what the second half look like?

Simple answer is that it doesn't. It has to search both conditions (via parallel queries which are then returned and duplicates merged) as such order does not really matter unless it were an $and and in this case the importance of order is in the index not the query as the query will be moved around to optimise for quickest path to results.

So in reality the way MongoDB works is that it shoots off a "query" per condition. This actually explains the behaviour: http://docs.mongodb.org/manual/reference/operator/query/or/#behaviors

When using indexes with $or queries, each clause of an $or can use its own index.

like image 84
Sammaye Avatar answered Sep 24 '22 23:09

Sammaye


Yes the order matters which is one strong reason for an array form of arguments which of course is ordered.

So basically this is referred to as "short circuit" evaluation. So only where the first condition does not match then is the next condition tested and so on.

So can best demonstrate with a collection like this:

{ "a": 1 },
{ "a": 2, "b": 1 }

And then the following query:

db.collection.find({ "$or": [ { "a": 1 }, { "b": 1 } ] })

Which of course finds both documents since even though the first does not have an element for "b" the first condition is met anyway. In the second document since the first failed then the second was used to match.

like image 28
Neil Lunn Avatar answered Sep 25 '22 23:09

Neil Lunn