Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get execution stats in MongoDB for a collection
To get execution stats in MongoDB for a collection, use the explain() method with "executionStats" parameter. This provides detailed performance information about query execution including execution time and documents examined.
Syntax
db.collection.find(query).explain("executionStats");
Sample Data
db.demo157.insertMany([
{"Status": "Active"},
{"Status": "InActive"}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5e354fdffdf09dd6d08539fc"),
ObjectId("5e354fe3fdf09dd6d08539fd")
]
}
Display all documents from the collection ?
db.demo157.find();
{ "_id": ObjectId("5e354fdffdf09dd6d08539fc"), "Status": "Active" }
{ "_id": ObjectId("5e354fe3fdf09dd6d08539fd"), "Status": "InActive" }
Example: Get Execution Stats
db.demo157.find({Status: { $in: ['Active','InActive'] }}).explain("executionStats");
{
"queryPlanner": {
"plannerVersion": 1,
"namespace": "test.demo157",
"indexFilterSet": false,
"parsedQuery": {
"Status": {
"$in": [
"Active",
"InActive"
]
}
},
"winningPlan": {
"stage": "COLLSCAN",
"filter": {
"Status": {
"$in": [
"Active",
"InActive"
]
}
},
"direction": "forward"
},
"rejectedPlans": []
},
"executionStats": {
"executionSuccess": true,
"nReturned": 2,
"executionTimeMillis": 18,
"totalKeysExamined": 0,
"totalDocsExamined": 2,
"executionStages": {
"stage": "COLLSCAN",
"nReturned": 2,
"executionTimeMillisEstimate": 0,
"works": 4,
"advanced": 2,
"docsExamined": 2
}
},
"ok": 1
}
Key Points
- executionTimeMillis: Total time taken to execute the query
- totalDocsExamined: Number of documents examined during execution
- nReturned: Number of documents returned by the query
- stage: Shows query execution method (COLLSCAN indicates collection scan)
Conclusion
The explain("executionStats") method provides comprehensive performance metrics for MongoDB queries. Use it to analyze query efficiency and identify optimization opportunities for better database performance.
Advertisements
