1. Home
  2. MongoDB
  3. MongoDB Tutorial
  4. MongoDB – count() method

MongoDB – count() method

MongoDB count() Method

Use db.collection.count() method to count the number of documents available in a collection.

Syntax:

db.COLLECTION_NAME.count(query)  

Example – Count Total Documents

Use the following command from mongo shell to count the number of available documents in users collection.

> db.users.count();

Output:

8

Example – Count with Find

You can also use count() function with other functions like find.

> db.users.find({"user_name":"rahul"}).count()

Output:

1
Tags , , ,
Was this article helpful to you? Yes 5 No 2
Advertisement