I have recently modified a complex multi-field query to use
$and instead of simply assigning the fields as direct properties of the query. The idea being to sequence things so it fails fast, instead of applying every condition, even if we know it should not be, if the previous condition is already false.
For example, if we have a collection of people with a schema looking as follows:
- Favourite Colour
Then a condition such as:
- people.address.country: ‘AU’
- people.address.city: ‘Melbourne’
- people.name: ‘john’
My understanding, based on documentation, is that without the
$and all the conditions will be evaluated, even if the previous conditions have already evaluated to false.
I am now being challenged to show that there is a performance improvement in doing this, so I have started writing a test case for this, but can anyone confirm my understanding is correct and whether there is any existing documentation or test cases showing the performance gain?