Optimizing your CRUD Operations

In earlier chapters, it was said that INDEX is only scanned if we are using a index subset or complete index in search in our CRUD operation… But in this chapter, INDEX scan is still done even when query has keys in following order: (Address.Zipcode, cuisine).sort(stars) but index was created as (cuisine, stars, address.zipcode)… How Mongo still scanned through index when search keys and index keys are not in order at all… Its confusing.

Hi @rohit-kumar,

The query optimiser will reorganise the fields in attempt to find the index that’s best suited for the job. What this means is, in the query predicate find({Address.Zipcode, cuisine}), the order doesn’t matter because it will be reordered (if necessary), however in the sort part sort(stars), the order matters.

Now let’s imagine that we have a second index (address.zipcode: 1). In this scenario, the query optimiser will still use the first index because it covers more relevant fields that will make the query more optimal, and therefore it will reorder the fields in the query predicate as before.