mongodb runtime_error ai_generated true

MongoServerError: query pattern not covered by any index

ID: mongodb/query-pattern-not-covered-by-index

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2024-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
mongodb-5.0 active
mongodb-6.0 active
mongodb-7.0 active

Root Cause

A query's filter or sort pattern does not match any existing index, forcing a collection scan.

generic

中文

查询的过滤或排序模式与任何现有索引不匹配,导致全集合扫描。

Official Documentation

https://www.mongodb.com/docs/manual/indexes/#indexes-and-query-performance

Workarounds

  1. 90% success Create a compound index that matches the query filter and sort fields in the correct order.
    Create a compound index that matches the query filter and sort fields in the correct order.
  2. 95% success Use explain() to analyze the query and determine the optimal index.
    Use explain() to analyze the query and determine the optimal index.

中文步骤

  1. Create a compound index that matches the query filter and sort fields in the correct order.
  2. Use explain() to analyze the query and determine the optimal index.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Creating an index on a different field does not help; the index must match the query pattern.

  2. 85% fail

    Using hint() to force an unrelated index still results in a scan.