- Reference >
mongo
Shell Methods >- Cursor Methods >
- cursor.count()
cursor.count()¶
On this page
Definition¶
-
cursor.
count
()¶ Counts the number of documents referenced by a cursor. Append the
count()
method to afind()
query to return the number of matching documents. The operation does not perform the query but instead counts the results that would be returned by the query.The
count()
method has the following prototype form:The
count()
method has the following parameter:Parameter Type Description applySkipLimit
Boolean Optional. Specifies whether to consider the effects of the cursor.skip()
andcursor.limit()
methods in the count. By default, thecount()
method ignores the effects of thecursor.skip()
andcursor.limit()
. SetapplySkipLimit
totrue
to consider the effect of these methods.See also
MongoDB also provides the shell wrapper
db.collection.count()
for thedb.collection.find().count()
construct.See also
Behavior¶
On a sharded cluster, count()
method can result in an inaccurate count if
orphaned documents exist or if a
chunk migration is in progress.
To avoid these situations, on a sharded cluster, use the
$group
stage of the db.collection.aggregate()
method to $sum
the documents. For example, the following
operation counts the documents in a collection:
To get a count of documents that match a query condition, include the
$match
stage as well:
See Perform a Count for an example.
Examples¶
The following are examples of the count()
method.
Example
Count the number of all documents in the orders
collection:
Example
Count the number of the documents in the orders
collection with the field ord_dt
greater than new
Date('01/01/2012')
:
Example
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)
: