The main objective of indexing is to speed up processing of queries and to speed up data manipulation operations that require database search before manipulations on data.
Indexing can also be used to enforce uniqueness constraints on the given key-values pairs in a collection of documents. Creating a unique index blocks insertion of the documents with the same given key-value pairs
Indexing of collections of JSON documents significantly reduce amount of time needed to access the documents. Indexing eliminates sequential read of an entire collection of JSON documents. With an index only some of the documents must be read.
The simplest version of an index is a "single-key" index. Such index is the most appropriate for the queries that use {"key":"value"} templates (patterns)
For the queries that have query templates (patterns) over multiple keys, like for example {$and:[{"key1":"value1"},{"key2":"value2"}]} a compound index is the best option.
A compound index has some advantages and disadvantages. The main disadvantage ia higher storage consumption, Additionally, a compound index requires more frequent modifications because probability of modification of a compound key is higher probabilities of modification of single key indexes. A modification of a value associated with one of the keys of a compound key always delays data manipulation operations.
The main advantage of a compound index key is such that it can be used for a wider range of queries than two single key indexes.
If we have a compound index on (key1, key2) then the second index on key1 is not really needed to process queries that use {"key1":"value"} templates (patterns), however it may still speed up an access a bit.
If we have a compound index on (key1, key2) then the second index on key2 speeds up access a lot however the queries that use {"key2":"value"} templates (patterns) still can be processed with the index.
It means that, like in relational database systems an order of keys in a compound index may have a significant impact on performance and consumption of persistent storage.
Fo example, a compound index on (last-name, date-of-birth) is perfect for the queries over multiple keys like {$and:[{"last-name":"Bond"},{"date-of-birth":"01-JAN-1960"}]}. If in such a case an index is implemented as B*-tree then one vertical traversal is required to filter the documents on the given condition.
The same index can be quite effectively used for a query over a single key {"last-name":"Bond"}. Then a vertical traversal of B*-Tree index is required and and later one horizontal traversal of B*-tree leaf level limited few data block.
The same index can be less effectively used (but still better than full scan of a collection of BSON documents) for a query over a single key {"date-of-birth":"01-JAN-1960"}. Then a horizontal traversal of a complete B*-tree leaf level is required.