MongoDB Compound Index Practice
Free MongoDB compound index practice: read one index and one query, then tap whether the index can support the filter, whether it can support the sort, or which key order follows the ESR guideline. Check my answer names the slip, such as a skipped prefix or a range key placed before the sort key.
The collections, fields and values are made up for practice. "Can support" is the MongoDB manual's wording: the query planner may still choose another plan.
Practise the exam
Working towards a MongoDB certification? Start with the free real exam questions, then get the full pack.
Index FAQ
What is an index prefix in MongoDB?
An index prefix is a beginning subset of the fields of a compound index. For an index on item, location and stock, in that order, the prefixes are item alone and item with location. The MongoDB manual says compound indexes support queries on all fields included in the index prefix.
Can a compound index support a query that skips the first field?
No. In the manual's example the index on item, location and stock cannot support queries on location, on stock, or on location and stock, because without the item field none of those combinations is a prefix.
Does the order of fields in the find filter matter?
No. The order of keys in the index matters, not the order you write the fields in the query. The manual says MongoDB's indexing strategy eliminates any need to arrange exact match fields in a particular order.
What if the filter uses the first and third index fields but not the second?
The index can still support the query through the first field, since that field is a prefix. The manual notes it is less efficient than an index on just those two fields: MongoDB filters the scanned keys by the third field.
When can a compound index support a sort?
When the sort keys are listed in the same order as in the index, every sort direction matches the index key pattern or every one is its inverse, and the sort keys are an index prefix. A sort on keys that are not a prefix also works when the query has equality conditions on all the index keys before them.
Can a sort run against the direction of the index?
Yes, when every key is reversed. An index on a ascending and b descending can support a sort on a descending and b ascending, which is the inverse of its key pattern. It cannot support a sort where one key matches the index and the other is reversed.
What happens when no index can support the sort?
MongoDB must perform an in-memory sort operation on the data. The manual says sort operations that use an index often have better performance than in-memory sorts.
What is the ESR guideline?
Equality, Sort, Range: put the fields the query matches exactly first, then the sort fields, then the range fields. The manual also describes the other order, range before sort, for a range predicate that is very selective, at the cost of an in-memory sort.
Is $ne an equality or a range operator?
A range operator. The manual says inequality operators such as $ne or $nin are range operators, not equality operators, so under ESR a field tested with $ne goes after the sort keys.
Sources
- MongoDB Database Manual, Compound Indexes: index prefixes, the field combinations a compound index can and cannot support, and the query on the first and third fields.
- MongoDB Database Manual, Use Indexes to Sort Query Results: sort keys in index order, directions that match the index key pattern or its inverse, sorting on an index prefix, sorting on a non-prefix subset with equality conditions on the keys before it, and in-memory sorts.
- MongoDB Database Manual, The ESR (Equality, Sort, Range) Guideline: equality fields first, sort before range to avoid in-memory sorts, and $ne as a range operator.
- Checked 21 November 2026. The tool's own approach: the collections, fields and values are invented, every filter field is an index key, and a sort key never has an equality condition in the filter. Left out because these pages give no exact rule for them or they need their own drill: a sort that skips an index key, $in, $regex, multikey, hashed, text, wildcard, partial and sparse indexes, collation, index intersection and covered queries. An independent brute force re-solver agreed with the drill on 12,000 generated problems. "Can support" follows the manual: the query planner may still choose another plan. This is an independent practice tool, not made or endorsed by MongoDB.
Checked 21 November 2026 against the MongoDB Database Manual.
- DrillTableau order of operations practiceTap filters and calculations in the order Tableau runs them, or work out what a small view shows with a context filter, FIXED, top N or table calculation.
- DrillJira JQL practiceTap the issues a JQL query returns from 8 sample issues, with != and NOT IN on empty fields, IS EMPTY, parentheses and ORDER BY.
- DrillGitHub Actions job outcome practiceRead a workflow's needs, if and continue-on-error and tap the jobs that are skipped.