In this article, I am going to discuss the count() function in MongoDB. The count() function in MongoDB helps us get the count of the documents inside a collection or view. It returns the count of those documents that would match a find() query for the collection or view.
However, instead of performing the find() operation, this method counts and provides the number of results that match a query.
This guide aims to provide a quick example of how to use the count() function in MongoDB for your benefit. So, let us get started!
The count() Function in MongoDB Syntax
Let us take a quick look at the count() function in MongoDB:
db.collection.count(query, options)
cursor.count()
The options parameter is the optional parameter for modifying the count operation.
Using the count() Function in MongoDB
Let us get started by looking at some examples.
Let us take a look at our collection first:
> db.drones.find().pretty()
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
"utility" : [
"Natural Resource Exploration",
"Remote sensing",
"Real estate and construction",
"Recreation",
"Delivery"
],
"onSale" : false,
"name" : "Nimbari Gryphon Medeta 65",
"price" : 77500,
"weight" : "77 kilograms",
"additionalDetails" : {
"material" : "carbon fiber",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Cinematography"
]
}
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
"utility" : [
"Natural Resource Exploration",
"Remote sensing",
"Real estate and construction",
"Recreation",
"Delivery"
],
"onSale" : false,
"name" : "X-Strimmer Eye",
"price" : 23500,
"weight" : "24 kilograms",
"additionalDetails" : {
"material" : "glass fiber",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Cinematography"
]
}
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
"utility" : [
"Natural Resource Exploration",
"Remote sensing",
"Real estate and construction",
"Recreation",
"Delivery"
],
"onSale" : false,
"name" : "Khai Balemosh Shefqa TRX",
"price" : 120500,
"weight" : "80 kilograms",
"additionalDetails" : {
"material" : "aluminum",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
}
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
"utility" : [
"Natural Resource Exploration",
"Remote sensing",
"Real estate and construction",
"Recreation",
"Delivery"
],
"onSale" : false,
"name" : "Sifinist Croma AX",
"price" : 99500,
"weight" : "97 kilograms",
"additionalDetails" : {
"material" : "lithium",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
}
}
{
"_id" : ObjectId("61673f46b34f185eb7b2bf10"),
"utility" : [
"Natural Resource Exploration",
"Remote sensing",
"Real estate and construction",
"Recreation",
"Delivery"
],
"onSale" : false,
"name" : "Drovce Finnifield FR-7",
"price" : 87600,
"weight" : "13 kilograms",
"additionalDetails" : {
"material" : "polysterene",
"moreUses" : [
"Precision Agriculture",
"Land Inspection",
"Water Inspection",
"Videography"
]
}
}
We have 5 documents.
Using count() Function with a Single Expression
Let us use a single operation with the count() function in MongoDB.
Suppose, we want to know how many drones are priced more than 80000.
> db.drones.find( { price: { $gt: 80000 } } ).count()
3
It says we have 3 such documents and it’s true.
Using the count() Function with Multiple Expressions
Let us use the count() function with multiple expressions.
Let’s say we want to know how many documents have the onSale value as false and price is greater than 90000.
> db.drones.find( { onSale: false, price: { $gt: 90000 } } ).count()
2
It shows we have 2 such documents and it is true.
Counting all Documents in a Collection
Let us use the count() function in MongoDB to count all documents of a collection.
> db.drones.count()
5
With the find() function
> db.drones.find().count()
5
Conclusion
The count() function in MongoDB helps us get the count of the documents inside a collection or view. It returns the count of those documents that would match a find() query for the collection or view.
Learn to use the count() function in MongoDB with multiple examples.
Noteworthy References
- https://docs.mongodb.com/manual/reference/method/db.collection.count/#mongodb-method-db.collection.count
- https://docs.mongodb.com/v4.4/reference/method/db.collection.count/