Types of MongoDB Indexes: Single Field, Compound, Multikey, Text & More
A complete guide to every MongoDB index type — single field, compound, multikey (array), geospatial, text, and wildcard indexes — with syntax and examples for each.
Types of MongoDB Indexes
MongoDB offers several index types, each suited to different data shapes and query patterns:
- Single Field Index
- Compound Index
- Multikey Index
- Geospatial Indexes
- Text Index
- Wildcard Index
1. Single Field Index
An index on one field, ascending (1) or descending (-1).
db.students.createIndex({ "roll": 1 })
2. Compound Index
Indexes multiple fields together. MongoDB sorts by the first field, then by the second within each group of the first, and so on — useful when your queries regularly filter or sort by the same combination of fields.
db.collection.createIndex({ field1: 1, field2: 1 })
db.student3.createIndex({ age: 1, name: 1 })
db.student3.find().sort({ age: 1, name: 1 })
// documents are ordered by age first, then by name within the same age
3. Multikey Index
Automatically created when you index a field that holds an array. MongoDB indexes every element of the array individually, so a query can match on any single value inside it.
db.student4.createIndex({ marks: 1 })
db.student4.find({ marks: [40, 70, 90] })
4. Geospatial Indexes
Used to query location-based data (coordinates). MongoDB provides 2d (legacy flat coordinates) and 2dsphere (spherical/Earth-like geometry) indexes.
db.industries.createIndex({ location: "2dsphere" })
5. Text Index
Enables full-text search over string content in a field.
db.student5.createIndex({ name: "text" })
db.student5.find({ $text: { $search: "gupta" } })
6. Wildcard Index
Indexes an entire subtree of fields under a key, matched with .$** — useful when the shape of your sub-documents is unpredictable (many similar but not-quite-uniform fields).
db.student6.createIndex({ "marks.$**": 1 })
db.student6.find({ "marks.mark1": 60 })
db.student6.find({ "marks.mark2": 60 })
// both queries use the same wildcard index
By default, a wildcard index excludes _id unless you explicitly include it.
Common Errors & Troubleshooting
- Creating a compound index in the wrong field order — a compound index on
{a:1, b:1}helps queries filtering onaalone, oraandbtogether, but does not help a query filtering onbalone — field order matters. - Expecting a text index to support partial-word matching —
$textsearch matches whole (stemmed) words, not substrings — searching"data"won't match"database"the way aLIKE '%data%'-style pattern would. - Only one text index allowed per collection — unlike other index types, MongoDB only permits a single text index per collection (though it can span multiple fields).
Key Takeaways
- Compound indexes speed up queries that filter/sort on multiple fields together — field order in the index matters.
- Indexing an array field automatically becomes a multikey index — no special syntax needed.
- Text and geospatial indexes unlock query operators (
$text,$near, etc.) that don't work without them.