MongoDB Count

MongoDB count() returns the number of results in a specified collection or number of documents that match in a find() query.

Syntax of count()

db.collection.count(query, options)

Here, count returns only number of results that match a query. The parameter 'query' is the selection criteria and the options parameter is the extra options, like - limit, skip, hint and are optional.

Example

The following statement returns total number of students in a collection.

db.students.count();




MongoDB count() with find()

We can also use count() with find() query. The given statement returns total number of students of class '5C'.

db.students.find({"class": "5C"}).count();
mongodb count





Read more articles