Think of indexes as a set of key value pairs, where each key is the value of the field that we indexed on, and the value (of the key) is the document itself. You can have multiple indexes on the same collection. Like an index in the back of a book, index keys are stored in order. Because of this, we don't need to look at every single index entry. MongoDB uses a B-Tree to store this information. Without an index, a new insertion will mean that a new comparison is needed, but with an index, not every insertion means that a new comparison is needed. As a result, querying is faster with an index. O^ log n
vs O^ n
.
Indexes are not without overhead however. Whenever a document is changed or deleted, the B-Tree needs to be updated. You shouldn't not have so many indexes that it slows down the CRUD operations. You should have enough indexes to make the queries fast, no more and no less.