Navigation
This version of the manual is no longer supported.

db.collection.count()

db.collection.count()

The db.collection.count() method is a shell wrapper that returns the count of documents that would match a find() query; i.e., db.collection.count() method is equivalent to:

db.collection.find(<query>).count();

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

The db.collection.count() method can accept the following argument:

Parameters:
  • query (document) – Specifies the selection query criteria.

Consider the following examples of the db.collection.count() method

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

    db.orders.count()
    

    The query is equivalent to the following:

    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.count( { ord_dt: { $gt: new Date('01/01/2012') } } )
    

    The query is equivalent to the following:

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

See also

cursor.count()