For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

How to Retrieve Query Plan and Execution Statistics

The MongoDB Search query returns information about the $search query plan and execution statistics when the query is run with the explain method enabled. When you run a query with explain, MongoDB Search returns a BSON document containing statistics and metadata that describes how the query was run internally in Lucene.

Tip

Visualize Query Plans in the Playground

Generate and view explain plan summaries with the MongoDB Search Playground.

db.<myCollection>.explain("<verbosity>").aggregate([
{
$search: {
"<operator>": {
"<operator-options>"
}
}
}
])

Verbosity mode controls the behavior of explain and the amount of information returned. Value can be one of the following, in order of decreasing verbosity:

Information about the query plan including the stats fields, which contain execution statistics for the query. Includes partial execution data captured during plan selection.

Information about the query plan including the stats fields, which contain execution statistics for the query.

queryPlanner (default)

Information about the query plan. Does not include the stats fields, which contain execution statistics for the query.

The query with the explain method returns the following fields in the stages.$_internalSearchMongotRemote object:

Option
Type
Purpose

mongotQuery

document

Contains the query that you ran.

explain

document

Contains the explain results. To learn more, see Explain Results.

internalMongotBatchSizeHistory

array of integers

Contains the size of each batch of documents that mongot returned to mongod.

The explain method returns a BSON document with the following fields in the explain document.

Option
Type
Necessity
Purpose

collectors

document

Optional

Describes the execution statistics for the collector. If indexPartitionExplain is present, this is nested within each indexPartitionExplain instead of at the top-level.

highlight

document

Optional

Details about the highlight execution for the query. This is returned only if you specified highlighting in the query.

indexPartitionExplain

array of documents

Optional

Contains per index partition details. This is only returned if you configured two or more index partitions.

metadata

document

Optional

Contains useful metadata.

query

document

Optional

Describes the execution statistics for the query. This is not returned. If indexPartitionExplain is present, this is nested within each indexPartitionExplain, instead of at the top-level.

resultMaterialization

document

Optional

Details related to retrieving per-document data from Lucene after query execution. This is not returned for queryPlanner verbosity mode.

resourceUsage

document

Optional

Details the resource usage of the query when it was executed. This is not returned for queryPlanner verbosity mode.

The collectors BSON document contains the following fields:

Field
Type
Necessity
Purpose

allCollectorStats

document

Required

Statistics of all collectors of the query. Statistics reported represent either the maximum value across all collectors used in the query or a sum of the statistic across all the sub-collectors. The timing statistics are summed to reflect the total time spent across all collectors for the entire query. To learn more, see allCollectorStats.

facet

document

Optional

Breakdown of the query that specifies facet. To learn more, see facet.

sort

document

Optional

Breakdown of the query that specifies sort. To learn more, see sort.

The allCollectorStats BSON document describes collector statistics across all collectors specified in the query, including facet and sort. It contains the following keys:

Field
Description

collect

Tracks the duration and number of results collected by the collector.

competitiveIterator

Statistics tracking the total duration and the number of times a competitiveIterator was requested from the collector.

setScorer

Statistics tracking the total duration and number of times a scorer was set on the collector.

The facet is a BSON document that details query and execution statistics when you specify a facet in the query. It contains the following fields:

Option
Type
Necessity
Purpose

collectorStats

document

Optional

Shows statistics only for the facet collector. The keys in here are identical to the allCollectorStats. You can evaluate the difference between this and the allCollectorStats to determine the sort collector execution statistics if you used sort in the query.

createCountsStats

document

Optional

Shows statistics related to creating the internal Lucene object that holds all of the facet groupings. It contains generateFacetCounts field, which returns the statistics tracking the total duration and number of times mongot generates the Lucene object that holds all of the facets and their counts.

stringFacetFieldCardinalities

document

Required

Maps the field being faceted on to their cardinalities for both the documents that matched the query and across the entire Lucene index. It provides the following cardinality information for each field:

  • queried - Represents the facet cardinality of the field across all queried documents.

  • total - Represents the facet cardinality of the field across all documents in the index.

The sort is a BSON document that details query and execution statistics when you specify a sort in the query. It contains the following fields:

Option
Type
Necessity
Purpose

stats

document

Optional

Tracks statistics related to sort execution across all sort fields. It contains the following fields:

  • comparator - Shows statistics and metadata around how the Lucene sorts the results.

    • setBottom - Returns how often the current least competitive sort value is updated with a new and more competitive value. When sorting, MongoDB Search keeps track of the bottom value and if a new value surpasses the current bottom value in relevance or competitiveness, this metric is incremented.

    • compareBottom - Returns the timing and invocation statistics related to comparing the current bottom value with a candidate bottom value.

    • compareTop - Returns the timing and invocation statistics related to comparing the current top value with a candidate top value.

    • setHitsThresholdReached - Returns the timing and invocation statistics related to when the maximum number of results (limit) was reached.

    • competitiveIterator - Returns the timing and invocation statistics related to iterating (only) over documents that are likely to compete for top spots in the sorted results.

    • setScorer - Returns the timing and invocation statistics related to computing the score and relevance of documents.

  • prunedResultIterator - Shows the invocations and timing statistics of nextDoc and advance method calls on Lucene's competitiveIterator.

    This is not present in the output if it performs a mixed data type sort.

    It contains only the nextDoc field.

fieldInfos

document

Required

Maps the field being sorted on to the list of data types present in the index for the field.

usesIndexSort

boolean

Optional

Indicates whether the query can benefit from the index sort. A query benefits when its sort fields match the index sort or form a prefix of it. If value is:

  • true, the query is eligible to use the sorted-index optimization.

  • false, the query can't benefit from the sorted index optimization.

This is absent in the output if the query doesn't specify a sort or if the index doesn't have a sort specification.

The highlight is a BSON document that details query and execution statistics when you specify highlighting in the query. It contains the following fields:

Option
Type
Necessity
Purpose

resolvedHighlightPaths

List<String>

Required

List of all the highlighted fields. If you specified a wildcard path in the highlight section of your query, contains a list of fully resolved document paths.

stats

QueryExecutionArea

Optional

Invocation and timing statistics related to setting up and executing highlights. It contains the following fields:

  • setupHighlight - Time it took for setting up the internal Lucene objects used during the executeHighlight phase.

  • executeHighlight - Time it took to run highlighting over the matching documents.

the indexPartitionExplain contains Explain Results for each index partition. The top-level collectors and query are inside the explain information of each index partition and absent at the top-level.

The metadata contains helpful metadata, such as the following:

Field
Type
Necessity
Purpose

mongotVersion

String

Optional

Current version of mongot.

mongotHostName

String

Optional

Human readable label that identifies the mongot host.

indexName

String

Optional

MongoDB Search index used in the query.

cursorOptions

Document

Optional

Cursor options given to mongot.

totalLuceneDocs

Integer

Optional

Total number of index objects in the index including deleted documents.

The query BSON document describes the execution statistics for the query. It contains the following fields:

Field
Type
Necessity
Purpose

path

string

Optional

Path to the operator, only if it isn't the root.

type

string

Required

Name of the Lucene Query that the MongoDB Search operator created. See query for more information.

analyzer

string

Optional

MongoDB Search analyzer used with the query.

args

document

Required

Lucene query information. See query for more information.

stats

document

Optional

stats for the query if explain ran with executionStats or allPlansExecution verbosity.

The explain response of a search command contains information about the query executed with that command. The response in the args field includes structured details of what Lucene queries MongoDB Search executed to satisfy a $search query.

This section contains:

  • Some of the Lucene queries that MongoDB Search operators create

  • Lucene query options that are included in the structured summary

  • Lucene query structured summary example for each Lucene query type

Note

About the Examples

The examples in this section are based on queries run against the sample datasets with the queryPlanner verbosity mode. In the example response, the:

  • mongotQuery field shows the MongoDB Search operator and the query that was run.

  • explain.type field shows the Lucene Query that the operator created.

For complete examples, see Examples.

BooleanQuery

For Lucene BooleanQuery, the structured summary includes details on the following options:

Field
Type
Necessity
Description

must

Optional

Clauses which must match.

mustNot

Optional

Clauses which must not match.

should

Optional

Clauses which should match.

filter

Optional

Clauses which must all match.

minimumShouldMatch

Integer

Optional

The minimum number of should clauses which must match.

The following example shows the explain response for a query run against the sample_airbnb.listingsAndReviews collection.

1{
2 "stages" : [
3 {
4 "$_internalSearchMongotRemote" : {
5 "mongotQuery" : {
6 "compound" : {
7 "must" : [ {
8 "compound" : {
9 "should" : [ {
10 "text" : {
11 "query" : "historic",
12 "path" : "summary"
13 }
14 },
15 {
16 "text" : {
17 "query" : "Portugal",
18 "path" : "address.country"
19 }
20 },
21 {
22 "text" : {
23 "query" : "railway",
24 "path" : "transit"
25 }
26 } ]
27 }
28 } ],
29 "mustNot" : [ {
30 "text" : {
31 "query" : "Apartment",
32 "path" : "property_type"
33 }
34 } ]
35 }
36 },
37 "explain" : {
38 "type" : "BooleanQuery",
39 "args" : {
40 "must" : [ {
41 "path" : "compound.must",
42 "type" : "BooleanQuery",
43 "args" : {
44 "must" : [ ],
45 "mustNot" : [ ],
46 "should" : [
47 {
48 "path" : "compound.must.compound.should[0]",
49 "type" : "TermQuery",
50 "args" : {
51 "path" : "summary",
52 "value" : "historic"
53 }
54 },
55 {
56 "path" : "compound.must.compound.should[1]",
57 "type" : "TermQuery",
58 "args" : {
59 "path" : "address.country",
60 "value" : "portugal"
61 }
62 },
63 {
64 "path" : "compound.must.compound.should[2]",
65 "type" : "TermQuery",
66 "args" : {
67 "path" : "transit",
68 "value" : "railway"
69 }
70 }
71 ],
72 "filter" : [ ],
73 "minimumShouldMatch" : 0
74 }
75 } ],
76 "mustNot" : [ {
77 "path" : "compound.mustNot",
78 "type" : "TermQuery",
79 "args" : {
80 "path" : "property_type",
81 "value" : "apartment"
82 }
83 } ],
84 "should" : [ ],
85 "filter" : [ ],
86 "minimumShouldMatch" : 0
87 }
88 }
89 },
90 ...
91 },
92 ...
93 ],
94 ...
95}
ConstantScoreQuery

For constant score queries, the structured summary includes details on the following options:

Field
Type
Necessity
Description

query

Required

Child of the ConstantScoreQuery.

FunctionScoreQuery

For Lucene FunctionScoreQuery queries, the structured summary includes details on the following options:

Field
Type
Necessity
Description

scoreFunction

string

Required

Scoring expression used in the query.

query

Required

The query.

LatLonPointDistanceQuery

For Lucene LatLonPointDistanceQuery queries, the response contains an stats only.

LatLonShapeQuery

For Lucene LatLonShapeQuery queries, the response contains an stats only.

LongDistanceFeatureQuery

For Lucene LongDistanceFeatureQuery, the response contains an stats only.

MultiTermQueryConstantScoreWrapper

For Lucene MultiTermQueryConstantScoreWrapper queries, the structured summary includes details on the following arguments:

Field
Type
Necessity
Description

queries

Required

List of queries.

PhraseQuery

For Lucene PhraseQuery queries, the structured summary includes details on the following arguments:

Field
Type
Necessity
Description

path

String

Required

Indexed field to search.

query

String

Required

String or strings to search for.

slop

Number

Required

Allowable distance between words in the query phrase.

PointRangeQuery

For Lucene PointRangeQuery queries, the structured summary includes details on the following arguments:

Field
Type
Necessity
Description

path

String

Required

Indexed field to search.

representation

String

Optional

Numeric representation. Queries over date-typed data do not include representation.

gte

Number

Optional

Lower bound of the query.

lte

Number

Optional

Upper bound of the query.

TermQuery

For term queries, the structured summary includes details on the following arguments:

Field
Type
Necessity
Description

path

String

Required

Indexed field to search.

value

String

Required

String to search for.

Default

Lucene queries that are not explicitly defined by another Lucene query are serialized using the default query. The structured summary includes details on the following option:

Field
Type
Necessity
Description

queryType

String

Required.

Type of Lucene query.

The explain response for executionStats and allPlansExecution verbosity modes includes a stats field that contains information on how much time a query spends in various stages of query execution.

The timing breakdown describes execution statistics pertinent to an areas of query execution. The following fields show the timing breakdown:

Field
Type
Description

millisElapsed

Long

Approximate wall-clock time elapsed performing tasks in this area including the amount of time the children of the query spent in this area. The value is approximate number of milliseconds elapsed while performing tasks in this area.

invocationCounts

Document

Number of invocations of tasks included in this area. The value is a map of task names to their invocation count.

Statistics are available for the following areas of query:

Option
Description

context

Statistics related to the execution of the Lucene query. There are two tasks whose invocation counts are enumerated in this area:

  • createScorer: Scorer iterates over documents and generates a score for each document. Invocations of createScorer create the object responsible for scoring. Note that time associated with this task is not time spent actually scoring documents. Count includes the number of scorerSupplier invocations.

  • createWeight: Weight stores state associated with a query and IndexSearcher. Count includes the number of createWeight invocations.

The time spent in this area is related to the structure of the query, and is not based on the number of results that are iterated through and scored.

For example:

"context" : {
"millisElapsed" : NumberDouble(4.934751),
"invocationCounts" : {
"createWeight" : NumberLong(1),
"createScorer" : NumberLong(10)
}
}

match

Statistics related to iterating over and matching result documents. This statistic shows the time it takes to determine which document is the next match. Time spent matching results can vary significantly depending on the nature of the query. There are two tasks whose invocation counts are enumerated in this area:

  • nextDoc: Requests to advance to the next document of the result set. This involves identifying and moving past skips, or other tasks necessary to find the next match. Count includes the number of nextDoc and advance invocations.

  • refineRoughMatch: Performs a more thorough match. Some queries execute in a two-phase process where a document is first "roughly" matched, and is checked with a second, more thorough phase only after satisfying the first rough match. The refineRoughMatch task is the second phase of the two-phase process. Count includes the number of refineRoughMatch invocations.

For example:

"match" : {
"millisElapsed" : NumberDouble(4.901597),
"invocationCounts" : {
"nextDoc" : NumberLong(541),
"refineRoughMatch" : NumberLong(0)
}
}

score

Statistics related to scoring documents in the result set. There are two tasks whose invocation counts are enumerated in this area:

  • score: Scores each document in the result set. Count includes the number of score invocations.

  • setMinCompetitiveScore: Ignores documents whose score is less than the given value. Indicates that a query may have been able to reduce the number of scoring operations performed by ignoring documents with scores below some uncompetitive threshold. Count includes the number of setMinCompetitiveScore invocations.

For example:

"score" : {
"millisElapsed" : NumberDouble(3.931312),
"invocationCounts" : {
"score" : NumberLong(536),
"setMinCompetitiveScore" : NumberLong(0)
}
}

The resultMaterialization document shows the amount of time mongot takes to accomplish the following:

  1. Retrieve result data stored in Lucene in the form of either _id or storedSource.

  2. Serialize the data into BSON format before sending it to mongod.

To learn more, see stats.

The resourceUsage document shows the resource used for running the query. It contains the following fields:

Field
Type
Necessity
Purpose

majorFaults

Long

Required

Number of major page faults, which occur when the system can't find the required data in memory resulting in reading from the backing store such as disk, during query execution.

minorFaults

Long

Required

Number of minor page faults, which occur when the data is in the page cache, but hasn't yet been mapped to the process' page table.

userTimeMs

Long

Required

Amount of CPU time, in milliseconds, spent in user space.

systemTimeMs

Long

Required

Amount of CPU time, in milliseconds, spent in system space.

maxReportingThreads

Integer

Required

Maximum number of threads that mongot used during query execution across all batches. For non-concurrent explain queries, the value is 1.

numBatches

Integer

Required

Total number of batches that mongot was requested when processing the query.

The following examples use the movies collection in the sample_mflix database.

Tip

If you've already loaded the sample dataset, refer to the MongoDB Search Quick Start tutorial to create an index definition and run MongoDB Search queries.

The following example uses different operators to query the title field with the allPlansExecution verbosity mode.

db.movies.explain("allPlansExecution").aggregate([
{
$search: {
"text": {
"path": "title",
"query": "yark",
"fuzzy": {
"maxEdits": 1,
"maxExpansions": 100,
}
}
}
}
])

The following example uses different operators to query the title field with the queryPlanner verbosity mode.

db.movies.explain("queryPlanner").aggregate([
{
$search: {
"text": {
"path": "title",
"query": "yark",
"fuzzy": {
"maxEdits": 1,
"maxExpansions": 100,
}
}
}
}
])

For queries that specify a $limit stage in the pipeline, the explain results include the mongotDocsRequested metric, which shows the number of documents that mongod requested from mongot.

Example

{
"mongotQuery": {},
"explain": {},
"limit": <int>,
"sortSpec": {},
"mongotDocsRequested": <int>,
}

The following example uses the autocomplete to query the title field with the executionStats verbosity mode.

1db.movies.explain("executionStats").aggregate([
2 {
3 "$search": {
4 "autocomplete": {
5 "path": "title",
6 "query": "pre",
7 "fuzzy": {
8 "maxEdits": 1,
9 "prefixLength": 1,
10 "maxExpansions": 256
11 }
12 }
13 }
14 }
15])

To learn more about the explain response elements, see Explain Results.