count() - Find the number of documents command

Finds the number of documents in a collection.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. count(query)

Command parameters

query
This optional parameter specifies a filter for selecting a subset of documents and returns a count.

Example

Example 1: Get the number of documents in the books collection:
db.books.count()
Sample output is as follows:
15
Example 2: Get the number of documents for an author:
db.books.count({author: "Tolkien, J.R"})
Sample output is as follows:
2