Introduction to indexes
An index is a special data structure that enables faster access to data in a collection, much like an old-school paper encyclopedia index. It is an ordered list of references to the actual contents—the documents—which allows MongoDB to query much faster, often by orders of magnitude. Indexes store values of a specific field or a set of fields, ordered by value. As you will see later in The equality, sort, range (ESR) rule section, MongoDB can return sorted results using the ordering of the index itself.
MongoDB indexes use a data structure known as B-tree, a self-balancing tree data structure that maintains sorted data, and allows sequential access, searches, insertions, and deletions in logarithmic time. The index can be thought of as a list of key-value pairs, where each key is a value of index, and the value of the key-value pair is the document itself. Like an index of a book, the keys are stored in order, and associated with one or more...