Facebook Pixel

When to Use Indexes in MongoDB (and When Not To)

Indexes speed up reads but slow down writes. Here is when to use them and when not to.

When to Use Indexes in MongoDB (and When Not To)

Indexes speed up reads but slow down writes. Here is when to use them and when not to.

When to Use Indexes

  • You query by a field often. If you look up users by email, index email.
  • You sort by a field often. If you sort messages by createdAt, index createdAt (or a compound index with the filter field).
  • You need uniqueness. Use a unique index on email, username, or other unique fields.
  • You paginate. Cursor-based pagination needs an index on the cursor field.
  • You filter on multiple fields together. Use a compound index.
  • You do full-text search. Use a text index.
  • You do geo queries. Use a 2dsphere index.

When Not to Use Indexes

  • The collection is small. For < 1000 documents, scans are fast. Indexes add overhead without benefit.
  • You rarely query by that field. Every index slows writes. If you never query by it, do not index it.
  • Writes are very frequent. Every index adds write overhead. For high-throughput writes, minimize indexes.
  • The query is already fast. Use explain() to confirm. If a query scans few docs without an index, adding one does not help.

How to Decide

  1. List the queries you run.
  2. For each, check with explain() how many docs it scans.
  3. If a query scans many docs, add an index for it.
  4. Re-run explain() to confirm the index is used.
  5. Repeat as the app evolves.

Indexes Have a Cost

Every index adds:

  • Write overhead (each insert/update/delete updates the index).
  • Storage (the index takes space).
  • Memory (MongoDB tries to keep indexes in RAM).

Do not index every field. Index the queries you actually run.

Compound vs Single Indexes

A compound index on (A, B) can serve queries on A and queries on A+B. So one compound index can replace two single indexes (on A and on B). But it cannot serve queries on B alone (prefix rule).

The Takeaway

Use indexes when you query, sort, need uniqueness, paginate, or filter on multiple fields. Do not index when the collection is small, you rarely query by that field, writes are very frequent, or the query is already fast. Use explain() to decide. Indexes have a write, storage, and memory cost.

When you query or sort by a field often, need uniqueness, paginate, filter on multiple fields together, do full-text search, or do geo queries. Use explain() to confirm a query scans many docs before adding an index.

When the collection is small (< 1000 docs), you rarely query by that field, writes are very frequent, or the query is already fast. Every index adds write, storage, and memory overhead. Do not index every field.

List the queries you run. For each, check with explain() how many docs it scans. If a query scans many docs, add an index for it. Re-run explain() to confirm. Repeat as the app evolves.

Partially. A compound index on (A, B) can serve queries on A and queries on A+B. So it can replace a single index on A. But it cannot serve queries on B alone (prefix rule). You might still need a separate index on B.

Write overhead (each insert/update/delete updates the index), storage (the index takes space), and memory (MongoDB tries to keep indexes in RAM). Do not index every field; index the queries you actually run.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.