34

I would like to know how to count the number of documents in a collection. I tried the follow

var value = collection.count();
&&
var value = collection.find().count()
&&
var value = collection.find().dataSize()

I always get method undefined.

Can you let me know what is the method to use to find the total documents in a collection.

3
  • 1
    The second should work... When this is the mongo shell, you might have to prefix your collection with db. like this: db.collection.find().count()
    – Philipp
    Nov 3, 2014 at 18:02
  • It does not like count(). I get the error below - Ganesh TypeError: undefined is not a function at C:\software\Enide-Studio\workspace\nodeexpress-mongo\node_modules\mongodb\lib\mongodb\collection\commands.js:55:5 at C:\software\Enide-Studio\workspace\nodeexpress-mongo\node_modules\mongodb\lib\mongodb\db.js:1183:7 Nov 3, 2014 at 18:13
  • I think this way is better for a specific collection: stackoverflow.com/a/69168420/7399485
    – Saulo M
    Sep 13, 2021 at 20:03

8 Answers 8

33

Traverse to the database where your collection resides using the command:

use databasename;

Then invoke the count() function on the collection in the database.

var value = db.collection.count();

and then print(value) or simply value, would give you the count of documents in the collection named collection.

Refer: http://docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell/

1
  • 1
    which ever collection count you are looking for call in mongo shell directly as db.salary.count() it will return the count. Sep 27, 2017 at 9:37
24

If you want the number of documents in a collection, then use the count method, which returns a Promise. Here's an example:

let coll = db.collection('collection_name');
coll.count().then((count) => {
    console.log(count);
});

This assumes you're using Mongo 3.

Edit: In version 4.0.3, count is deprecated. use countDocument to achieve the goal.

13

Since v4.0.3 you can use for better performance:

db.collection.countDocuments()
1
  • 2
    Note that this is still not a performant operation. CountDocuments does a full collection scan and is a costly query on large collections that can cause potentially huge I/O spikes. Feb 18, 2020 at 10:29
5

Execute Mongo shell commands in single line for better results.

To get count of all documents in mongodb

var documentCount = 0; db.getCollectionNames().forEach(function(collection) { documentCount++; }); print("Available Documents count: "+ documentCount);

Output: Available Documents count: 9

To get all count of document results in a collection

db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("Results count for " + collection + ": "+ resultCount); });

Output:

Results count for ADDRESS: 250 
Results count for APPLICATION_DEVELOPER: 950
Results count for COUNTRY: 10
Results count for DATABASE_DEVELOPER: 1
Results count for EMPLOYEE: 4500
Results count for FULL_STACK_DEVELOPER: 2000
Results count for PHONE_NUMBER: 110
Results count for STATE: 0
Results count for QA_DEVELOPER: 100

To get all dataSize of documents in a collection

db.getCollectionNames().forEach(function(collection) { size = db[collection].dataSize(); print("dataSize for " + collection + ":"+ size); });

Output:

dataSize for ADDRESS: 250 
dataSize for APPLICATION_DEVELOPER: 950
dataSize for COUNTRY: 10
dataSize for DATABASE_DEVELOPER: 1
dataSize for EMPLOYEE: 4500
dataSize for FULL_STACK_DEVELOPER: 2000
dataSize for PHONE_NUMBER: 110
dataSize for STATE: 0
dataSize for QA_DEVELOPER: 100
3

Simply you can use

      Model.count({email: '[email protected]'}, function (err, count) {
      console.log(count);
    });
2

db.collection('collection-name').count() is now deprecated. Instead of count(), we should now use countDocuments() and estimatedDocumentCount(). Here is an example:

`db.collection('collection-name').countDocuments().then((docs) =>{
console.log('Number of documents are', docs);
    };`

To know more read the documentation:

https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/

https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/

1

Through MongoDB Console you can see the number of documents in a collection.

1.Go to mongoDB console and issue command "use databasename". To start the console go up to the bin folder of where MongoDB is installed and click on mongo.exe to start the mongoDB console e.g If the database is myDB then command is "use myDB"

2.Execute this command db.collection.count() collection is like table in RDBMS. e.g if your collection name is myCollection then the command is db.myCollection.count();

this command will print the size of the collection in the console.

0
db.collection.count(function(err,countData){

//you will get the count of number of documents in mongodb collection in the variable 
countdata

});

In place of collection give your mongodb collection name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.