When you license MongoDB, you typically kick-off your MongoDB relationship with the "Rapid Start" on-site training and consulting provided by folks from MongoDB Inc.
One piece of technical information presented on the topic of Indexing makes for a nice blog post that can be referred to again later. So let's jump right in.
Compound Indexes reference multiple fields from a collection in a single index structure. This is probably the most helpful type of index to create for optimizing a particular query, such as a query that searches for Customers.
The developer or DBA creating a compound index must determine the optimal field order within the index. The concept is to let selectivity drive the order of the fields.
- Order fields in a compound index from most selective to least selective
- Usually, this means equality fields (fields queried for a specific value) before range fields (e.g., date fields queried for a particular time period)
- When dealing with multiple equality values, start with the most selective
The desired sort order of the query results must be considered as well. And when we consider sorting, we can summarize the general rules of thumb for the optimal compound index as follows.
- Equality before range
- Equality before sorting
- Sorting before range