Introduction to Indexes
Databases can maintain and use indexes to make searches more efficient. In MongoDB, indexes are created on a field or a combination of fields. The database maintains a special registry of indexed fields and some of their data. The registry is easily searchable, as it maintains a logical link between the value of an indexed field and the respective documents in the collection. During a search operation, the database first locates the value in the registry and identifies the matching documents in the collection accordingly. The values in a registry are always sorted in ascending or descending order of the values, which helps during a range search and also while sorting the results.
To better understand how the index registry helps during searches, imagine you are searching for a theater by its ID, as follows:
db.theaters.find( {"theaterId" : 1009} )
When the query is executed on the sample_mflix
database, it returns a...