Docs Menu


On this page

  • Definition
  • Usage
  • Example
  • Parameters
  • Return Value

Return the number of documents in a collection or view that match the specified query filter.

To call the collection.count() action from a Function, get a collection handle with database.collection() then call the handle's count() method.

collection.count({ status: "Complete" })
.then(numDocs => console.log(`${numDocs} documents match the specified query.`))
.catch(err => console.error("Failed to count documents: ", err))

The collection.count() action has the following form:


Query Filter

query: <document>

Optional. A standard MongoDB query document that specifies which documents to count. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Specify an empty query filter ({}) or omit this parameter to count all documents in the collection.

Client Session

session: <ClientSession>
Optional. A client session that specifies the transaction context in which the operation occurs. To learn more, see Transactions.

The collection.count() action returns a Promise that resolves to the integer number of documents in the collection that match the query filter.

Promise<numDocs: Number>

Count Result

numDocs: <integer>
The number of documents in the collection that match the provided query filter.
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.