Skip to content

MongoDB - Indexing Limitations

MongoDB - Indexing Limitations & Considerations

Section titled “MongoDB - Indexing Limitations & Considerations”

While indexes are crucial for query performance, they are not a silver bullet. Understanding their limitations and trade-offs is key to building efficient and scalable applications. This chapter explores the critical considerations you must keep in mind when designing your indexing strategy.

Think of an index as a separate, specialized data structure. For every insert, update, or delete operation on an indexed collection, MongoDB must perform two writes: one to the collection itself and another to each of its indexes. This introduces a performance cost.

Consequently, collections with a high number of indexes will experience slower write speeds. This is a fundamental trade-off: faster reads vs. slower writes. It’s crucial to index only the fields you frequently query.

For write-heavy workloads, such as logging or IoT data ingestion, you should be especially selective about your indexes.

Indexes consume both storage space on disk and memory (RAM). In modern MongoDB versions using the WiredTiger storage engine, frequently accessed indexes and data are held in an in-memory cache for fast retrieval. The total size of your indexes directly impacts this cache.

If your indexes are too large to fit comfortably in the WiredTiger cache, MongoDB may need to read them from disk, which is significantly slower. You must ensure your server has sufficient RAM to support your working set (the data and indexes your application frequently accesses).

You can monitor index size using the db.collection.stats() command.

An index is most effective when it helps MongoDB select a small subset of documents. Certain query operators are inherently non-selective and cannot effectively use standard indexes:

  • Un-anchored Regular Expressions: A regex like /pattern/ requires scanning the entire index (or collection). However, a left-anchored regex like /^pattern/ can efficiently use an index on that field. For complex text search, the modern best practice is to use a dedicated Atlas Search index.
  • Negation Operators ($ne, $not, $nin): These operators are typically non-selective. A query for status: { $ne: 'archived' } might match 99% of your documents, forcing a full index scan. It’s often better to model data to query for what you do want, not what you don’t.
  • $where Clause: The $where operator executes arbitrary JavaScript on the server side. It is very powerful but cannot use indexes at all and should be avoided in performance-critical queries.

Never assume your query is using an index. Always verify it using the explain() method. A winning plan (winningPlan.stage) will show IXSCAN (index scan), while a losing plan will show COLLSCAN (collection scan).

db.users.find({ email: ‘test@example.com’ }).explain(‘executionStats’);

The total size of an index entry must be less than 1024 bytes. This limit applies to the BSON representation of the indexed field’s value. MongoDB will refuse to create an index on a collection if it contains existing documents that violate this limit. Similarly, it will reject any new document insertion or update where the indexed field’s value exceeds this limit.

This is most commonly an issue when indexing large strings or large array fields.

Be aware of these hard limits within MongoDB:

  • Indexes per Collection: A single collection can have a maximum of 64 indexes.
  • Fields in a Compound Index: A compound index (an index on multiple fields) can include a maximum of 32 fields.
  • Index Name Length: The maximum length for an index name is 128 characters (including the namespace and separators).
  • Over-indexing: Don’t index every field. This slows down writes and wastes memory. Analyze your application’s query patterns and index strategically.
  • Low-Cardinality Indexes: Avoid indexing fields with very few distinct values (e.g., a boolean isActive field). Such indexes are rarely selective enough to be useful.
  • Ignoring Compound Index Field Order: The order of fields in a compound index matters. Order them based on your queries: 1. Equality, 2. Sort, 3. Range.
  • Forgetting Background Builds: In production, always build indexes in the background ({ background: true }) to avoid blocking database operations. In modern MongoDB versions, this is the default behavior.