MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization
Parent: Global AI Hub Research Corpus · Published reference · snapshot 2026-10-19
↓ Facts as markdownall context files
Everything the scanned sources say about MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization: 145 source-anchored units across 3 sources, grouped by facet.
These notes link each claim to its source. Some citations identify archived research reports that are no longer hosted on this site. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
Definitions
- The intelligent assistant is an AI-powered tool designed for integrated MongoDB development support in MongoDB Compass. It answers natural language questions, assists with debugging errors, and provides guidance for performance optimization. [source]
Structure and components
- - Text indexes can consume significant RAM. They contain one index entry for each unique stemmed word in each indexed field for each document. [source]
- Text indexes support [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text) [queries](https://www.mongodb.com/docs/manual/text-search.md#std-label-text-query) on fields that contain string content. They also support [aggregation expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-qe-aggregation-operators) for encrypted string fields in [Queryable Encryption](https://www.mongodb.com/docs/manual/core/queryable-encryption.md#std-label-qe-manual-feature-qe) enabled collections. Text indexes improve performance when [source]
- | Scenario | Performance insight | | --- | --- | | You run a query or aggregation without an index. | Add an index to support the operation. | | You run an aggregation pipeline that uses a [`$lookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup.md#mongodb-pipeline-pipe.-lookup) stage. | Embed related data to avoid the need for a `$lookup` operation. | | You run a [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text) or [`$regex`](https://www.mongodb.com/docs/manual/reference/operator/query/regex.md#mongodb-query-op. [source]
- This example matches documents where `runtime` is greater than `1000` and the indexed fields contain `baseball` or `colorado`. It sorts by ascending `year`, then descending relevance score: [source]
- If the `movies` collection contains an index `{ year: 1, title: 1 }`, the following pipeline can use that index to find the first document of each group: [source]
- 1. The `movies` collection contains documents where the `metacritic` field is present in some documents and absent from others. Of the 21,349 documents, 6,964 have the `metacritic` field and 14,385 do not. [source]
- For example, a pipeline that consists of [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match), [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort), [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) can benefit from indexes at every stage: [source]
- Because the index contains all fields required by the query, MongoDB can both match the [query conditions](https://www.mongodb.com/docs/manual/tutorial/query-documents.md#std-label-read-operations-query-document) and return the results using only the index. [source]
- Contains information on the selection of the query plan by the [query optimizer.](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-read-operations-query-optimization) [source]
- | Field | Description | Applicable Stages | | --- | --- | --- | | `docsExamined` | Specifies the number of documents scanned during the query execution stage. | `COLLSCAN`, `FETCH` | | `keysExamined` | For query execution stages that scan an index `keysExamined` is the total number of in-bounds and out-of-bounds keys that are examined in the process of the index scan. If the index scan consists of a single contiguous range of keys, only in-bounds keys need to be examined. If the index bounds consists of several key ranges, the index scan execution process may examine out-of-bounds keys in orde [source]
- Starting in MongoDB 9.0, when you run `explain` in a mimumum of [executionStats](https://www.mongodb.com/docs/manual/reference/method/db.collection.explain.md#std-label-explain-method-executionStats) verbosity mode, aggregation stages that use the classic query execution engine output a `expressionEvaluationPeakMemoryBytes` field. This field contains the peak memory usage, in bytes, from expression evaluation during stage execution. [source]
How it works
- - When building large text indexes, ensure sufficient file descriptor limits. See [recommended settings.](https://www.mongodb.com/docs/manual/reference/ulimit.md#std-label-ulimit) [source]
- - Text indexes store individual words, not multi-word strings or word proximity information. Queries with multiple words run faster when the entire collection fits in RAM. [source]
- When explain is run in either executionStats or allPlansExecution verbosity mode, certain aggregation stages that may spill to disk have additional output. These stages include $sort, $group, $setWindowFields, $bucketAuto, $graphLookup, and $lookup (when using the slot-based execution engine). Starting in MongoDB 8.1, this also includes $text queries that use the TextOr execution stage. [source]
- Text indexes support `$text` queries on fields containing string content. Text indexes improve performance when searching for specific words or multi-word strings within string content. [source]
- - A sort on `{ <timeField>: ±1 }` uses a secondary index on `{ metaField: ±1, timeField: ±1 }` when there is a point predicate on `<metaField>`. [source]
- Last point queries are most performant when they use the [DISTINCT\_SCAN optimization](https://www.mongodb.com/docs/manual/reference/explain-results.md#std-label-explain-results). This optimization is only available when an index on `timeField` is descending. [source]
- Index hints cause MongoDB to use a specific index for a query. Some operations on time series collections can only take advantage of an index if that index is specified in a hint. [source]
- For example, the following query causes MongoDB to use the `timestamp_1_metadata.sensorId_1` index: [source]
- - [`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined) displays `3809` to indicate that MongoDB scanned 3,809 index entries. The number of keys examined match the number of documents returned, meaning that the [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) only had to examine index keys to return the results. The [`mongod`](https://www.mongodb.com/docs/manual/reference/program/mongod.md#mongodb-binary-bin.mongod) did not have to scan all [source]
- When run with an index, the query scans only as many index entries and documents as it returns, resulting in a more efficient query. [source]
- - When possible, put [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) at the beginning of the pipeline to use indexes that scan the matching documents in a collection. [source]
How-to and procedures
- To optimize your indexes, we recommend deploying [dedicated Search nodes](https://www.mongodb.com/docs/atlas/cluster-config/multi-cloud-distribution.md#std-label-configure-search-nodes), which allows you to scale your Atlas cluster and [`$search`](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search) workloads independently. Dedicated Search Nodes only run the `mongot` process and therefore improve the availability, performance, and workload balancing of the `mongot` process. [source]
- To improve query performance, you can create a [compound text index](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-text/create-text-index.md#std-label-compound-text-index-example) and include an equality match in your `$text` queries. If the compound index contains the field used in your equality match, the index scans fewer entries and returns results faster. [source]
- Create a secondary single-field index on the `timestamp` field: [source]
- To confirm that the sort operation used the Secondary Index, run the operation again with the `explain` option: [source]
- To confirm that the last point query used the secondary index, run the operation again using `explain`: [source]
- To confirm that the sort operation used the Secondary Index, run the operation again with the `.explain( "executionStats" )` option: [source]
- To confirm that the last point query used the secondary index, run the operation again using `.explain( "executionStats" )`: [source]
- To view the query plan selected, chain the [`cursor.explain("executionStats")`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) cursor method to the end of the **find** command: [source]
- To support the query on the `year` field, add an index on the `year` field: [source]
- To view the query plan statistics, use the [`explain()`](https://www.mongodb.com/docs/manual/reference/method/cursor.explain.md#mongodb-method-cursor.explain) method: [source]
- To support the query, add a [compound index](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-compound.md#std-label-index-type-compound). With compound indexes, the order of the fields matter. [source]
- To determine whether a pipeline uses indexes, review the query plan and look for `IXSCAN` or `DISTINCT_SCAN` plans. [source]
Examples and snippets
- ```javascript — `id: '<object-id>',` … [source]
- ```csharp — `await _collection?.Indexes.CreateOneAsync(new CreateIndexModel<BsonDocument>(` … [source]
- ```csharp — `// Indexes on ``timeField`` descending are more performant because they` … [source]
- ```csharp — `_collection?.Indexes.CreateOne(` … [source]
- ```text — `"explainVersion": "1",` … [source]
- ```csharp — `_collection?.Indexes.CreateOne(new CreateIndexModel<BsonDocument>(` … [source]
- ```java — `collection.createIndex(Indexes.ascending("timestamp"));` … [source]
- ```java — `// Indexes on ``timeField`` descending are more performant because they` … [source]
- ```java — `collection.createIndex(Indexes.compoundIndex(` … [source]
- ```java — `collection.createIndex(Indexes.geo2dsphere("metadata.location"));` … [source]
- ```javascript — `// Indexes on ``timeField`` descending are more performant because they` … [source]
- ```python — `# Indexes on ``timeField`` descending are more performant because they` … [source]
- ```javascript — `queryPlanner: {` … [source]
- ```javascript — `db.runCommand(` … [source]
Measurements and reference values
- This alert might appear when Atlas automatically [upgrades your search indexes](https://www.mongodb.com/docs/search/performance/index-performance.md#std-label-index-create-and-update) to enable new features. Your cluster must have sufficient disk space for both the previous and new version of the index. If the rebuild increases disk space use to 90% or more, Atlas increases the cluster storage if you enabled auto-scaling. After the index upgrade completes, Atlas deletes the old version of the index, which frees up disk space. [source]
Problems, failure modes and limitations
- | Text Index | MongoDB Search Index | | --- | --- | | `db.movies.aggregate([ { $match: { year: {$gte: 2000, $lte: 2010}, $text: { $search: "poet" }, genres : { $eq: "Comedy" } } }, { "$sort": { "year": 1 } }, { "$limit": 3 }, { "$project": { "_id": 0, "title": 1, "genres": 1, "plot": 1, "year": 1 }, [source]
- Other $text restrictions: a query with $text cannot use hint(); text indexes do not improve sort performance; a multi-word phrase mixed with single terms matches only documents containing the full phrase; text indexes accept only binary comparison, so a collection with a non-simple collation needs collation {locale: 'simple'} on the text index; compound text indexes need equality conditions on key prefixes. [source]
- WARNING: If you shard a collection that already has a MongoDB Search index, you might experience a brief period of incomplete search results while the index is building on a shard. Also, if you add a shard for an already sharded collection that contains a MongoDB Search index, your search queries against that collection might return incomplete results until the initial sync process completes on the added shards. To learn more, see [initial sync process.](https://www.mongodb.com/docs/search/performance/index-performance.md#std-label-troubleshoot-initial-sync) [source]
- Text indexes cannot improve performance for sort operations. This restriction applies to both single-field and compound text indexes. [source]
- In a small collection like the one used in the preceding example, there isn't a noticeable difference in performance between single-field and compound text indexes. However, in larger collections, increased index entry scans can noticeably hinder performance. For best performance, create text indexes that limit the number of index entries scanned to best fit your equality matches. [source]
- Case-insensitive indexes do not improve performance for [`$regex`](https://www.mongodb.com/docs/manual/reference/operator/query/regex.md#mongodb-query-op.-regex) queries, as the `$regex` operator is not collation-aware and therefore cannot take advantage of such indexes. [source]
- In 9.0 the classic engine reports expressionEvaluationPeakMemoryBytes in explain at executionStats verbosity or higher for $project, $addFields, $set, $unset, $replaceRoot, $replaceWith, $match, $bucketAuto, $graphLookup and $lookup. A failed operation logs 'Query exceeded the memory limit' with code 12932700. [source]
- | Status | Description | | --- | --- | | `BUILDING` | The following scenarios can cause an index to be in the `BUILDING` state: MongoDB is building the index or re-building the index after an edit.; MongoDB Search cannot keep up with indexing changes to the collection. In this case, Atlas rebuilds the index in the background. When the index is in the `BUILDING` state: For a new index, MongoDB Search cannot use the index for queries until the index build is complete.; For an existing index, MongoDB Search uses the old index definition for queries until the index rebuild is complete. An index in [source]
- A case-sensitive $regex can use an index range scan only when the pattern is an anchored simple prefix (starts with ^ or \A followed by simple characters); the scan stops once the prefix no longer matches. Unanchored patterns and patterns with leading wildcards cannot narrow the index range. [source]
- | `$exists` Query | Using a Sparse Index | Using a Non-Sparse Index | | --- | --- | --- | | `{ $exists: true }` | Most efficient. MongoDB can make an exact match and does not require a `FETCH`. | More efficient than queries without an index, but still requires a `FETCH`. | | `{ $exists: false }` | Cannot use the index and requires a `COLLSCAN`. | Requires a `FETCH`. | [source]
- - Use the [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match), [`$limit`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit.md#mongodb-pipeline-pipe.-limit), and [`$skip`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/skip.md#mongodb-pipeline-pipe.-skip) stages to restrict the documents that enter the pipeline. [source]
- | Field | Type | Description | | --- | --- | --- | | `distanceField` | string | The output field that contains the calculated distance. To specify a field within an embedded document, use [dot notation.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-dot-notation) This field is required for queries on timeseries collections. Starting in MongoDB 8.1, `distanceField` is optional for queries on non-timeseries collections. | | `distanceMultiplier` | number | Optional. The factor to multiply all distances returned by the query. For example, use the `distanceMultiplier` to conver [source]
- Starting in MongoDB 9.0, if an indexed field becomes a multikey field while a query that references the field is running, the query might fail with a QueryKilledError. An indexed field becomes multikey when you insert or update a document so the field contains an array value. [source]
- | Field | Description | | --- | --- | | [into](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge.md#std-label-merge-into) | The output collection. Specify either: The collection name as a string to output to a collection in the same database where the aggregation is run. For example:`into: "myOutput"`; The database and collection name in a document to output to a collection in the specified database. For example:`into: { db:"myDB", coll:"myOutput" }` If the output collection does not exist, `$merge` creates the collection: For a [replica set](https://www.mongodb.com/docs [source]
Comparisons and alternatives
- - [MongoDB Search](https://www.mongodb.com/docs/atlas/atlas-search/) provides improved performance and functionality compared to on-premises text search. [source]
- A compound text index on the `department` and `description` fields limits the index keys scanned to only documents within the specified `department`. The compound text index provides improved performance compared to a single-field text index on the `description` field. [source]
- MongoDB's 9.0 performance claims versus 8.0: up to 35% faster find-one, 30% faster update-one, 20% higher transactional throughput, nearly 2x throughput on large deployments. The vendor says these come from internal testing and vary by workload. Percona could not find methodology or raw data, and press coverage reports no benchmark configuration. [source]
- 8.2.0 known issues: up to 15% change-stream performance degradation versus 8.0, and a $group distinct-scan-on-sparse-index bug that hides null/missing results (SERVER-110423) fixed in 8.2.2. [source]
- To drop a [text](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-text.md#std-label-index-type-text) index, specify the index name instead of the index specification document. [source]
- The second compound index, `{ rated: 1, year: 1 }`, is therefore the more efficient index for supporting the example query, as the MongoDB server only needs to scan `235` [`index keys`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined) to find all matching documents using this index, compared to `246` when using the compound index `{ year: 1, rated: 1 }`. [source]
Facts and statements
- - Building a text index is similar to building a large [multikey index](https://www.mongodb.com/docs/manual/core/indexes/index-types/index-multikey.md#std-label-index-type-multi-key) but takes longer than building an ordered (scalar) index on the same data. [source]
- > Derived 2026-10-19; topic-filtered subset of mongodb.com-docs-query.md: Server manual pages about queries, indexes, aggregation, time series, profiling, explain, parameters and 7.x–9.0 release notes, plus Compass query/explain/index pages and Atlas performance/query pages. Third-party text; private reading aid; never publish. [source]
- The examples in the following sections use queries against the `sample_mflix.movies` collection in the [sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-sample-data) to illustrate the improvements to flexibility and performance that MongoDB Search offers over [`$text`](https://www.mongodb.com/docs/manual/reference/operator/query/text.md#mongodb-query-op.-text). You can run the queries from both examples using the following indexes: [source]
- You can create a text index to improve the performance of text queries run on a collection containing documents or embedded documents with text in multiple languages. [source]
- By default, the `default_language` for text indexes is `english`. To improve the performance of non-English `$text` queries, you can specify a different default language associated with your text index. [source]
- Index use and performance for `$regex` queries depends on whether the query is case-sensitive or case-insensitive. [source]
- MongoDB 8.2 removes `profileOperationResourceConsumptionMetrics`. Therefore, code depending on [operationMetrics](https://www.mongodb.com/docs/manual/reference/explain-results.md#std-label-executionStats) does not work. [source]
- Known issue at FCV 9.0: listIndexes reports collation { locale: 'simple' } for simple-collation indexes ([redacted]), so Mongoose syncIndexes() (also Doctrine ODM, Beanie) drops and rebuilds unchanged indexes. Workaround: do not call syncIndexes() at startup, or use Mongoose 9.10.4 / 8.24.5. Fix targeted for a future 9.0 patch. [source]
- The following example returns the index named `synonym-mappings` on the `movies` collection: [source]
- 3. Run the [atlas clusters search nodes create](https://www.mongodb.com/docs/atlas/cli/current/command/atlas-clusters-search-nodes-create.md#std-label-atlas-clusters-search-nodes-create) command with the `--file` option. [source]
- 3. Run the [atlas clusters search nodes update](https://www.mongodb.com/docs/atlas/cli/current/command/atlas-clusters-search-nodes-update.md#std-label-atlas-clusters-search-nodes-update) command with the `--file` option. [source]
- The `{ "content": "text" }` index only includes the `content` field, and does not return matches on non-indexed fields. For example, the following query searches the `blog` collection for the string `food`: [source]
- MongoDB documentation recommends rewriting $text-heavy queries as $search to gain flexibility and performance (language awareness, fuzzy matching, highlighting, autocomplete, synonyms, relevance scoring), with the gain growing as data size and query breadth grow. The same page calls $text adequate for simple, narrow searches, so migration is not advised for every query. [source]
- - Deleting many documents can fragment storage files, additionally impacting performance. You may need to run the [compact](https://www.mongodb.com/docs/manual/reference/privilege-actions.md#mongodb-authaction-compact) command on your collection or perform a [Initial Sync](https://www.mongodb.com/docs/manual/core/replica-set-sync.md#std-label-replica-set-initial-sync) to reclaim space and optimize storage. [source]
- For example, this [`$in`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/in.md#mongodb-expression-exp.-in) query benefits from a secondary index on `metadata.type`: [source]
- Sort operations on time series collections can use secondary indexes on the `timeField` field. Under certain conditions, sort operations can also use compound secondary indexes on the `metaField` and `timeField` fields. [source]
- The aggregation pipeline uses the [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) and [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) stages to determine which indexes a time series collection can use. An index can be used in the following scenarios: [source]
- - A sort on `{ <timeField>: ±1 }` uses a secondary index on `<timeField>`. [source]
- - A sort on `{ <metaField>: ±1, timeField: ±1 }` uses the default compound index on `{ <metaField>: ±1, timeField: ±1 }`. [source]
- The following command creates a compound secondary index on `metaField` (ascending) and `timeField` (descending): [source]
- The following last point query example uses the descending `timeField` compound secondary index created above: [source]
- On a time series collection, you can specify hints using either the index name or the index key pattern. To get the names of the indexes on a collection, use the [`db.collection.getIndexes()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.getindexes.md#mongodb-method-db.collection.getIndexes) method. [source]
- Starting in version 6.0 you can create 2dsphere indexes on the `timeField`, `metaField`, or measurement fields. For example, the following operation creates a 2dsphere index on the `location` field: [source]
- If there are [secondary indexes](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-secondary-index) on [time series collections](https://www.mongodb.com/docs/manual/core/timeseries-collections.md#std-label-manual-timeseries-collection) and you need to downgrade the feature compatibility version (FCV), you must first drop any secondary indexes that are incompatible with the downgraded FCV. For more information, see [`setFeatureCompatibilityVersion`.](https://www.mongodb.com/docs/manual/reference/command/setfeaturecompatibilityversion.md#mongodb-dbcommand-dbcmd.setFeatureCompati [source]
- The aggregation pipeline stages [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) and [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) determine which indexes a time series collection can use. An index can be used in the following scenarios: [source]
- - Sort on `{ <timeField>: ±1 }` uses a secondary index on [source]
- compound index on `{ <metaField>: ±1, timeField: ±1 }` [source]
- On a time series collection, you can specify hints using either the index name or the index key pattern. To get the names of the indexes on a collection, use `GetIndexes()`. [source]
- Starting in version 6.0 you can create `2dsphere` indexes. For example, the following operation creates a 2dsphere index on the `location` field: [source]
- Starting in version 6.0, you can create 2dsphere indexes on the `timeField`, `metaField`, or measurement fields. For example, the following operation creates a 2dsphere index on the `location` field: [source]
- The following example shows how MongoDB uses bounds intersection to define a smaller range of values to query, resulting in improved query performance. [source]
- The following example shows how MongoDB uses compound bounds to define a more efficient query constraint, resulting in improved query performance. [source]
- - Use sequentially increasing key values to improve insert performance. [source]
- Randomly generated key values may decrease a clustered collection's performance. [source]
- If you use the `_id` field in a range query, performance is improved. For example, the following query uses `_id` and [`$gt`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/gt.md#mongodb-expression-exp.-gt) to return the orders where the order date is greater than the supplied date: [source]
- 2. Create a [sparse index](https://www.mongodb.com/docs/manual/core/index-sparse.md#std-label-index-type-sparse) on the `metacritic` field: [source]
- 3. The following example counts the documents where the `metacritic` field has a value (including null) and uses the [sparse index:](https://www.mongodb.com/docs/manual/core/index-sparse.md#std-label-index-type-sparse) [source]
- - Create queries that your current indexes support to reduce the time needed to search for your results. [source]
- - Avoid creating documents with large array fields that require a lot of processing to search and index. [source]
- - [`queryPlanner.winningPlan.stage`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.queryPlanner.winningPlan.stage) displays `COLLSCAN` to indicate a collection scan. [source]
- - [`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned) displays `3809` to indicate that the winning query plan returns 3,809 documents. [source]
- - [`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined) displays `0` to indicate that this query is not using an index. [source]
- - [`executionStats.totalDocsExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalDocsExamined) displays `21349` to indicate that MongoDB had to scan all 21,349 documents in the collection to find the 3,809 matching documents. [source]
- The difference between the number of matching documents and the number of examined documents may suggest that the query might benefit from the use of an index to improve efficiency. [source]
- - [`queryPlanner.winningPlan.inputStage.stage`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.queryPlanner.winningPlan.inputStage) displays `IXSCAN` to indicate index use. [source]
- - [`executionStats.totalDocsExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalDocsExamined) display `3809` to indicate that MongoDB scanned 3,809 documents. [source]
- For example, add the following two compound indexes. The first index orders by `year` field first, and then the `rated` field. The second index orders by `rated` first, and then the `year` field. [source]
- MongoDB scanned `246` index keys ([`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined)) to return `235` matching documents ([`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned)). [source]
- MongoDB scanned `235` index keys ([`executionStats.totalKeysExamined`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.totalKeysExamined)) to return `235` matching documents ([`executionStats.nReturned`](https://www.mongodb.com/docs/manual/reference/explain-results.md#mongodb-data-explain.executionStats.nReturned)). [source]
- - An index on the `$match` query field efficiently identifies the relevant data [source]
- - An index on the sorting field returns data in sorted order for the `$sort` stage [source]
- - An index on the grouping field that matches the `$sort` order returns all of the field values needed for the `$group` stage, making it a covered query. [source]
- In some cases, the query planner uses a `DISTINCT_SCAN` index plan that returns one document per index key value. `DISTINCT_SCAN` executes faster than `IXSCAN` if there are multiple documents per key value. However, index scan parameters might affect the time comparison of `DISTINCT_SCAN` and `IXSCAN`. [source]
- During the `$match` stage, the server can use an index if `$match` is the first stage in the pipeline, after any optimizations from the [query planner.](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-query-plans-query-optimization) [source]
- During the `$sort` stage, the server can use an index if the stage is not preceded by a [`$project`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project), [`$unwind`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind.md#mongodb-pipeline-pipe.-unwind), or [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage. [source]
- During the `$group` stage, the server can use an index to quickly find the [`$first`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/first.md#mongodb-group-grp.-first) or [`$last`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/last.md#mongodb-group-grp.-last) document in each group if the stage meets both of these conditions: [source]
- - The pipeline [`sorts`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) and [`groups`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) by the same field. [source]
- - The `$group` stage only uses the [`$first`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/first.md#mongodb-group-grp.-first) or [`$last`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/last.md#mongodb-group-grp.-last) accumulator operator. [source]
- The server always uses an index for the `$geoNear` stage, since it requires a [geospatial index.](https://www.mongodb.com/docs/manual/geospatial-queries.md#std-label-index-feature-geospatial) [source]
- Additionally, stages later in the pipeline that retrieve data from other, unmodified collections can use indexes on those collections for optimization. These stages include: [source]
- - [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) followed by [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) at the start of the pipeline is equivalent to a single query with a sort, and can use an index. [source]
- MongoDB runs the [query optimizer](https://www.mongodb.com/docs/manual/core/query-plans.md#std-label-read-operations-query-optimization) to choose the winning plan, executes the winning plan to completion, and returns statistics describing the execution of the winning plan. [source]
- Querying *only* the index can be much faster than querying documents outside of the index. Index keys are typically smaller than the documents they catalog, and indexes are typically available in RAM or located sequentially on disk. [source]
- Time in milliseconds the query planner spent on query optimization. This result doesn't include time spent optimizing inner [`$lookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup.md#mongodb-pipeline-pipe.-lookup) queries. [source]
- A document that details the plan selected by the [query optimizer.](https://www.mongodb.com/docs/manual/core/query-plans.md) [source]
- A document that details the plan selected by the [query optimizer](https://www.mongodb.com/docs/manual/core/query-plans.md). MongoDB presents the plan as a tree of stages. [source]
- Array of candidate plans considered and rejected by the query optimizer. The array can be empty if there were no other candidate plans. [source]
- In such cases, during the initial read of the documents, MongoDB pages the documents into memory such that subsequent calls of the same count operation will have better performance. [source]
- For improved memory usage, we recommend using the default value of `true`. To learn more about improvements to performance and memory management, see [Upgraded TCMalloc.](https://www.mongodb.com/docs/manual/release-notes/8.0.md#std-label-8.0-tcmalloc-upgrade) [source]
- Starting in MongoDB 8.0, the default value of `tcmallocReleaseRate` is reduced to `0` due to a [tcmalloc upgrade](https://www.mongodb.com/docs/manual/release-notes/8.0.md#std-label-8.0-tcmalloc-upgrade) that prioritizes CPU performance over memory release. [source]
- Performance insights direct you to the [Intelligent Assistant in Data Explorer](https://www.mongodb.com/docs/atlas/atlas-ui/query-with-natural-language/data-explorer-ai-assistant.md#std-label-de-ai-assistant), which can provide additional help with your schema or queries. [source]
- Starting in MongoDB 9.0, `explain` outputs the following field: [source]
- For detailed examples with sample outputs, see [Performance Optimization.](https://www.mongodb.com/docs/mcp-server/local-mcp/examples.md#std-label-mcp-server-performance-examples) [source]
Related concepts
- index — is a related of MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization
- query optimization — is a hypernym of MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization
- Atlas Search vs text search — is a contrast of MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization
- release notes — is a part of MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization
- AI-assisted query optimization — is a part of MongoDB 8.1–9.0 query-engine changes and AI-assisted optimization
Children
- No children recorded.