发布于 2015-09-14 15:07:25 | 165 次阅读 | 评论: 0 | 来源: 网络整理
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. The count command takes the following prototype form:
{ count: <collection>, query: <query>, limit: <limit>, skip: <skip> }
The command fields are as follows:
Fields: |
|
---|
Consider the following examples of the count command:
Count the number of all documents in the orders collection:
db.runCommand( { count: 'orders' } )
In the result, the n, which represents the count, is 26 and the command status ok is 1:
{ "n" : 26, "ok" : 1 }
Count the number of the documents in the orders collection with the field ord_dt greater than new Date('01/01/2012'):
db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } }
} )
In the result, the n, which represents the count, is 13 and the command status ok is 1:
{ "n" : 13, "ok" : 1 }
Count the number of the documents in the orders collection with the field ord_dt greater than new Date('01/01/2012') skipping the first 10 matching records:
db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } },
skip: 10 } )
In the result, the n, which represents the count, is 3 and the command status ok is 1:
{ "n" : 3, "ok" : 1 }
注解
MongoDB also provides the cursor.count() method and the shell wrapper db.collection.count() method.