- Reference >
- Database Commands >
- count
count¶
-
count
¶ The
count
command counts the number of documents in a collection. The command returns a document that contains the count as well as the command status. Thecount
command takes the following prototype form:The command fields are as follows:
Fields: - count (String) – The name of the collection to count.
- query (document) – Optional. Specifies the selection query to determine which documents in the collection to count.
- limit (integer) – Optional. Specifies the limit for the documents matching the
selection
query
. - skip (integer) – Optional. Specifies the number of matching documents to skip.
Consider the following examples of the
count
command:Count the number of all documents in the
orders
collection:In the result, the
n
, which represents the count, is26
and the command statusok
is1
:Count the number of the documents in the
orders
collection with the fieldord_dt
greater thannew Date('01/01/2012')
:In the result, the
n
, which represents the count, is13
and the command statusok
is1
:Count the number of the documents in the
orders
collection with the fieldord_dt
greater thannew Date('01/01/2012')
skipping the first10
matching records:In the result, the
n
, which represents the count, is3
and the command statusok
is1
:
Note
MongoDB also provides the
cursor.count()
method and the shell wrapperdb.collection.count()
method.