MongoDB Compound Indexes Link to heading
These indexes index multiple fields in a collection. Compound indexes are core to query optimization, and can dramatically increase read performance.
Compound indexes collect and sort data from two or more fields. Data is grouped by the first field, and then by each subsequent field.
For example, a compound index for the userId
and score
fields, both sorted in ascending order.
userId | score |
---|---|
aa1 | 100 |
ca2 | 55 |
ca2 | 75 |
ca2 | 110 |
nb1 | 30 |
zyz | 90 |
First the index orders by
userId
, thenscore
Indexing commonly queried fields increases the likelihood of covered queries, which are queries that can be satisfied entirely using an index.
Links Link to heading
- [[mongodb-indexes.md|Indexes]]
- [[mongodb-esr-rule|ESR Rule]]