How to Perform $count Aggregation in MongoDB
Introduction
When you’re performing aggregation in MongoDB, there’s a lot going on behind the scenes. Documents go into an aggregation pipeline that consists of multiple stages; at each stage, the documents undergo some type of transformation. In some situations, you may need to count the documents that were passed to a certain stage of the pipeline and then pass that number to the next stage. We can accomplish this task using $count. In this article, we’ll take a closer look at the $count stage and learn how to use $count for aggregation in MongoDB.
Prerequisites
Before reading any further, be sure that the following prerequisites are in place:
- MongoDB must be properly installed in your system with the server running in the background.
- It’s helpful to have a basic understanding of MongoDB in order to follow along with the examples in this article.
MongoDB $count Aggregation
The MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents.
Shown below is the basic syntax of the $count operator:
1 | {$count: {<the_string>}} |
There a couple of important things to note about this syntax:
- First, we invoke the $count operator and then specify the string.
- In this syntax, ‘the_string’ represents the label or the name of the output field. It must be non-empty and cannot start with a dollar sign ‘$’ or dot ‘.’ character.
$count Aggregation Example
In this section, we’ll look at some examples of how to perform the $count aggregation in MongoDB.
We’ll start by creating a sample MongoDB collection that we can use for demo purposes.
We’ll create a product collections, which will contain a couple of documents:
1 2 3 4 5 6 7 8 | db.product.insertMany([ {"product_name" : "N95 Mask", "product_no" : "pr005", "product_description" : "face mask", product_qty : 500}, {"product_name" : "N306 Mask", "product_no" : "pr006", "product_description" : "respirator mask", product_qty : 340},, {"product_name" : "Surgical Gloves", "product_no" : "pr007", "product_description" : "hand gloves", product_qty : 340},, {"product_name" : "Coolo Goggles", "product_no" : "pr008", "product_description" : "Ski goggles", product_qty : 500},, {"product_name" : "N240 Gloves", "product_no" : "pr010", "product_description" : "Nonlatex gloves", product_qty : 700},, {"product_name" : "B3T Barrier", "product_no" : "pr011", "product_description" : "Breathing Barrier", product_qty : 500} ]); |
Our product collection should now look something like this:
1 2 3 4 5 6 | { "_id" : ObjectId("5e4ced7ca56f56c309df073f"), "product_name" : "N95 Mask", "product_no" : "pr005", "product_description" : "face mask", "product_qty" : 500 } { "_id" : ObjectId("5e4ced7ca56f56c309df0740"), "product_name" : "N306 Mask", "product_no" : "pr006", "product_description" : "respirator mask", "product_qty" : 340 } { "_id" : ObjectId("5e4ced7ca56f56c309df0741"), "product_name" : "Surgical Gloves", "product_no" : "pr007", "product_description" : "hand gloves", "product_qty" : 340 } { "_id" : ObjectId("5e4ced7ca56f56c309df0742"), "product_name" : "Coolo Goggles", "product_no" : "pr008", "product_description" : "Ski goggles", "product_qty" : 500 } { "_id" : ObjectId("5e4ced7ca56f56c309df0743"), "product_name" : "N240 Gloves", "product_no" : "pr010", "product_description" : "Nonlatex gloves", "product_qty" : 700 } { "_id" : ObjectId("5e4ced7ca56f56c309df0744"), "product_name" : "B3T Barrier", "product_no" : "pr011", "product_description" : "Breathing Barrier", "product_qty" : 500 } |
Now, let’s perform our aggregation. The command syntax is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | db.product.aggregate( [ { $match: { "product_qty": { $lt: 700 } } }, { $count: "product_on_the_budget" } ] ); |
Let’s take a closer look at what’s happening in this command:
- First, we use the $match aggregate to filter the documents where the value of ‘product_qty’ is less than 700. The result will then be passed to the next stage.
- The $count stage counts the documents that match the criteria from the previous stage. It returns that value, assigning it to a field called ‘product_on_the_budget’.
1 | { "product_on_the_budget" : 5 } |
Conclusion
If you’re planning to perform any type of aggregation in MongoDB, it’s important to understand how the various stages of the aggregation pipeline work. In this article, we focused on the $count stage. We explained how to perform $count aggregation in MonogDB and provided an example to illustrate the process. With these instructions and examples, you’ll be able to tackle aggregation in your own MongoDB database operations.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started