Navigation
This version of the manual is no longer supported.

cursor.count()

cursor.count()

The count() method counts the number of documents referenced by a cursor. Append the count() method to a find() query to return the number of matching documents, as in the following prototype:

db.collection.find().count()

This operation does not actually perform the find(); instead, the operation counts the results that would be returned by the find().

The count() can accept the following argument:

Parameters:

MongoDB also provides the shell wrapper db.collection.count() for the db.collection.find().count() construct.

Consider the following examples of the count() method:

  • Count the number of all documents in the orders collection:

    db.orders.find().count()
    
  • Count the number of the documents in the orders collection with the field ord_dt greater than new Date('01/01/2012'):

    db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).count()
    
  • Count the number of the documents in the orders collection with the field ord_dt greater than new Date('01/01/2012') taking into account the effect of the limit(5):

    db.orders.find( { ord_dt: { $gt: new Date('01/01/2012') } } ).limit(5).count(true)