Loading

Quipoin Menu

Learn • Practice • Grow

mongodb / Indexing for Performance
tutorial

Indexing for Performance

Imagine searching for a book in a library without a catalog system. You'd have to check every single shelf! That's what MongoDB does without indexes – it scans every document (called a collection scan). **Indexes** are like the library's catalog system – they make searches lightning fast.

What are Indexes?

Indexes are special data structures that store a small portion of the collection's data in an easy-to-search form. They support efficient execution of queries by reducing the number of documents that need to be examined.

Think of an index like the index at the back of a book. Instead of reading every page to find a topic, you look in the index and go directly to the right page.

Without Index – Collection Scan

When you run a query without an index, MongoDB scans every document in the collection:
db.users.find({ email: "john@example.com" })

If you have 1 million users, MongoDB checks all 1 million documents. This is slow and inefficient.

Creating a Single Field Index
<!-- Create an index on the email field -->
db.users.createIndex({ email: 1 })

<!-- 1 means ascending order, -1 would be descending -->

Now when you search by email, MongoDB uses the index to find the document instantly.

Checking if an Index is Used

Use `explain()` to see how MongoDB executes a query:
db.users.find({ email: "john@example.com" }).explain("executionStats")

Look for `stage: "IXSCAN"` (index scan) vs `stage: "COLLSCAN"` (collection scan).

Compound Indexes

Often you query on multiple fields. A compound index includes multiple fields.
<!-- Create index on (category, price) -->
db.products.createIndex({ category: 1, price: -1 })

<!-- This index helps queries like: -->
db.products.find({ category: "electronics", price: { $lt: 100 } })

Unique Indexes

To prevent duplicate values in a field, create a unique index:
db.users.createIndex({ email: 1 }, { unique: true })

Now you cannot insert two users with the same email.

Listing All Indexes
db.users.getIndexes()

Dropping an Index
<!-- Drop by name -->
db.users.dropIndex("email_1")

<!-- Drop by specification -->
db.users.dropIndex({ email: 1 })

Practical Example: Performance Comparison
<!-- Insert 10,000 test documents -->
for (let i = 0; i < 10000; i++) {
  db.test.insertOne({ x: i, y: i % 100 });
}

<!-- Query without index -->
db.test.find({ x: 5000 }).explain("executionStats")
<!-- Notice totalDocsExamined: 10000 -->

<!-- Create index -->
db.test.createIndex({ x: 1 })

<!-- Query with index -->
db.test.find({ x: 5000 }).explain("executionStats")
<!-- Notice totalDocsExamined: 1 -->

Two Minute Drill

  • Indexes make queries faster by avoiding collection scans.
  • Create single field indexes with `createIndex({ field: 1 })`.
  • Compound indexes support queries on multiple fields.
  • Use `explain()` to check if your index is being used.
  • Unique indexes prevent duplicate values.
  • Indexes speed up reads but slow down writes slightly (because the index must be updated).

Need more clarification?

Drop us an email at career@quipoinfotech.com