Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO:
MongoDB vCore
The count
command is used to count the number of documents in a collection that match a specified query. This command is useful for obtaining quick statistics about the data stored in your collections, such as the number of documents that meet certain criteria.
The syntax for the count
command is as follows:
db.collection.count(query, options)
query
: A document specifying the selection criteria using query operators.options
: Optional. A document specifying options, such aslimit
andskip
.
Here are some examples to demonstrate the usage of the count
command:
To count all documents in the stores
collection:
db.stores.count({})
[mongos] StoreData> db.stores.countDocuments({})
60570
To count the number of stores with a specific _id
store ID:
db.stores.count({ "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" })
[mongos] StoreData> db.stores.count({ "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" })
1
To count the number of stores that have a specific promotion event:
db.stores.count({ "promotionEvents.eventName": "Incredible Discount Days" })
[mongos] StoreData> db.stores.count({ "promotionEvents.eventName": "Incredible Discount Days" })
2156
To count the number of stores located at a specific latitude and longitude:
db.stores.count({ "location.lat": -2.4111, "location.lon": 72.1041 })
[mongos] StoreData> db.stores.count({ "location.lat": -2.4111, "location.lon": 72.1041 })
1
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB