Labs ICT
โญ Pro Login

Introduction to Indexes

Why indexes matter and how they speed up queries.

Introduction to Indexes

So you want to learn about MongoDB indexes? Great choice. Think about this โ€” when you need to find a specific word in a textbook, do you read every single page? Of course not. You flip to the index at the back, find the page number, and go straight there. That's exactly what a MongoDB index does for your data.

Without indexes, MongoDB has to scan every single document in a collection to find the ones matching your query. That's called a collection scan, and it's about as slow as it sounds. With millions of documents, this can tank your application performance. Indexes solve this problem by giving MongoDB a fast lookup path to your data.

How Indexes Work Internally

Under the hood, MongoDB uses a B-tree data structure for most indexes. A B-tree keeps data sorted and allows searches, insertions, and deletions in logarithmic time. In plain English, that means finding a document takes roughly the same amount of time whether your collection has 1,000 or 100 million documents.

When you create an index on a field, MongoDB builds this sorted structure that maps field values to the documents containing them. Then when you run a query, MongoDB walks through the B-tree instead of scanning every document. It's like having a GPS instead of driving around randomly looking for a house.

db.users.createIndex({ email: 1 })

This single line creates an index on the email field. Now queries filtering by email will be blazing fast instead of crawling through your entire users collection.

The Default _id Index

Here's something interesting โ€” MongoDB already gives you an index for free. Every collection automatically gets an index on the _id field. That's why queries like db.users.findOne({ _id: ObjectId("...") }) are always fast, no matter how big your collection grows.

You cannot remove this default index, and you shouldn't want to. The _id field is your document's primary key, and having it indexed is essential. Think of it like the foundation of a house โ€” it's always there and everything else builds on top of it.

When to Create Indexes

Not every field needs an index. Creating too many indexes can actually slow things down because MongoDB has to maintain all of them on every write operation. It's like having too many bookmarks in a book โ€” eventually they start getting in the way.

Focus on fields that you frequently query, sort by, or use in aggregations. A good rule of thumb: if a field appears in your find() queries regularly, it probably deserves an index. If you're sorting by a field often, that's another strong candidate.

You'll also want to index fields used in $lookup pipelines and match stages in aggregations. These are performance hotspots where indexes make a dramatic difference.

db.orders.find({ userId: ObjectId("..."), status: "pending" }).sort({ createdAt: -1 })

For this query, you'd want to consider an index that covers both the filter fields and the sort field. But we'll get into index strategies in later lessons.

๐Ÿงช Quick Quiz

What is an index in MongoDB?