Creating Single Field and Compound Indexes in MongoDB
Indexes in MongoDB are like indexes in a book — they help you find data faster. Without an index, MongoDB must scan every document in a collection to find matches, which can be slow for large datasets.
This tutorial explains how to create single field and compound indexes, when to use them, and how they can improve performance.
What is an Index?
An index is a special data structure that stores a small portion of the collection’s data in an easy-to-traverse format. MongoDB uses this index to quickly identify documents that match a query.
Why Are Indexes Important?
Imagine a collection with 1 million products. Searching for { price: 1000 }
without an index requires MongoDB to check each document. An index on price
helps MongoDB jump directly to matching documents.
Creating a Single Field Index
Let’s say we have a products
collection with the following documents:
db.products.insertMany([
{ name: "Laptop", price: 70000, category: "Electronics" },
{ name: "Phone", price: 30000, category: "Electronics" },
{ name: "Shoes", price: 4000, category: "Fashion" },
{ name: "Backpack", price: 2000, category: "Accessories" }
]);
To create an index on the price
field:
db.products.createIndex({ price: 1 });
Output:
{ "createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 }
Explanation: The 1
indicates an ascending index. You can also use -1
for descending. MongoDB now maintains a fast lookup table based on the price
field.
How to Check Which Indexes Exist
db.products.getIndexes();
Output:
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" }, { "v": 2, "key": { "price": 1 }, "name": "price_1" } ]
Intuition Check
Q: Does creating an index speed up all types of queries?
A: No. Indexes speed up queries that filter or sort based on the indexed field. If your queries never involve price
, then indexing it offers no benefit and adds unnecessary overhead.
Creating a Compound Index
A compound index includes multiple fields and supports queries that filter on one or more of those fields in the same order.
Let’s create an index on category
and price
:
db.products.createIndex({ category: 1, price: -1 });
Output:
{ "createdCollectionAutomatically": false, "numIndexesBefore": 2, "numIndexesAfter": 3, "ok": 1 }
Explanation: This index helps queries that use category
first, then price
. For example:
db.products.find({ category: "Electronics" }).sort({ price: -1 });
Important Rule: Prefix Property
MongoDB can use a compound index for queries that filter on the prefix fields. For { category: 1, price: -1 }
, the prefix is:
category
category + price
But not just price
by itself — that query won’t use the index efficiently.
Intuition Check
Q: Will the following query use the { category: 1, price: -1 }
index?
db.products.find({ price: { $lt: 50000 } });
A: No. Because price
is not the prefix field. MongoDB cannot skip the first indexed field.
Dropping an Index
If you no longer need an index, drop it using:
db.products.dropIndex("category_1_price_-1");
Best Practices
- Only index fields you frequently use in queries or sorting.
- Avoid indexing too many fields — it increases write overhead.
- Use compound indexes in the order your queries filter/sort.
Summary
Single field indexes are simple and useful for basic lookups. Compound indexes are powerful when your queries involve multiple fields — but order matters. Understanding how indexes work can greatly improve the performance of your MongoDB queries.