Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an explain for a MongoDB count?

Tags:

mongodb

In MongoDB, you can get an explanation for how a query was executed, with interesting performance information:

> db.people.find({ 'items' : { '$gte' : 1 } }).explain()

Can I get the same for a "count" (which is not a query, but a command)?

> db.people.count({ 'items' : { '$gte' : 1 } })
like image 868
Thilo Avatar asked Apr 13 '12 03:04

Thilo


People also ask

What is explain command in MongoDB?

The explain command provides information on the execution of the following commands: aggregate , count , distinct , find , findAndModify , delete , mapReduce , and update .

How do I count records in MongoDB collection?

Description. n = count( conn , collection ) returns the total number of documents in a collection by using the MongoDB connection. n = count( conn , collection ,'Query', mongoquery ) returns the total number of documents in an executed MongoDB query on a collection.

How does MongoDB count Atlas?

The Atlas Search count option adds a field to the metadata results document that displays a count of the search results for the query. You can use count to determine the size of the result set. You can use it in the $search or $searchMeta stage.


1 Answers

Mongo 3.0 introduced a new way of explaining non-cursor requests:

db.my_collection.explain().count()

See: http://docs.mongodb.org/manual/reference/method/db.collection.explain/#db.collection.explain

like image 126
marmor Avatar answered Sep 23 '22 15:09

marmor