⬅ Previous Topic
What are Indexes and Why Use ThemNext Topic ⮕
Multikey Indexes for Arrays⬅ Previous Topic
What are Indexes and Why Use ThemNext Topic ⮕
Multikey Indexes for ArraysIndexes 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.
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.
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.
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 });
{ "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.
db.products.getIndexes();
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_" }, { "v": 2, "key": { "price": 1 }, "name": "price_1" } ]
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.
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 });
{ "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 });
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.
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.
If you no longer need an index, drop it using:
db.products.dropIndex("category_1_price_-1");
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.
⬅ Previous Topic
What are Indexes and Why Use ThemNext Topic ⮕
Multikey Indexes for ArraysYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.