- Reference >
- Database Commands >
- count
count¶
-
count¶ The
countcommand counts the number of documents in a collection. The command returns a document that contains the count as well as the command status. Thecountcommand 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
countcommand:Count the number of all documents in the
orderscollection:In the result, the
n, which represents the count, is26and the command statusokis1:Count the number of the documents in the
orderscollection with the fieldord_dtgreater thannew Date('01/01/2012'):In the result, the
n, which represents the count, is13and the command statusokis1:Count the number of the documents in the
orderscollection with the fieldord_dtgreater thannew Date('01/01/2012')skipping the first10matching records:In the result, the
n, which represents the count, is3and the command statusokis1:
Note
MongoDB also provides the
cursor.count()method and the shell wrapperdb.collection.count()method.