Question

In an earlier question I found out that the MongoDB does multiple queries for each $or statement and that the duplicates are discarded in post processing. How is $and query implemented?

Is there some kind of optimization that if the first statement is FALSE that the document is discarded?

Was it helpful?

Solution

The $and statement is significantly different from the $or statement. Since $and is restrictive by definition, any single clause of the $and expression can be used as the single query to fetch documents for further evaluation. MongoDB examines available indices to optimize and choose the query to run. Each result is then evaluated against the AND of the remaining clauses. As you probably guessed, this is significantly easier than the $or statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top