9951 explained code solutions for 126 technologies


mongodbHow to use "explain" in MongoDB?


MongoDB's explain() method is used to provide information about how MongoDB executes a query. It can be used to analyze query performance and identify potential areas of improvement.

Example

db.collection.explain("executionStats")

Output example

{
    "queryPlanner" : {
        ...
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 0,
        "executionStages" : {
            ...
        },
        "allPlansExecution" : [ ]
    },
    "serverInfo" : {
        ...
    },
    "ok" : 1
}

The output of explain() contains the following information:

  • queryPlanner: The query plan MongoDB used to execute the query.
  • executionStats: Statistics about the query execution, such as the number of documents examined and the execution time.
  • serverInfo: Information about the server on which the query was executed.

Helpful links

Edit this code on GitHub