Home » Software Development » MongoDB » MongoDb Aggregate Count Example

About Adam Aijaz

Adam Aijaz
Adam got initial professional qualification from RMIT University Melbourne Australia, where he got Master in IT and Business, he got further Professional Certification in IT (CP) from Australian Computer Society (ACS) and Diploma in IT and Finance from Australis College, Brisbane. He got more than 15 years of IT experience in Software development on different technologies, he works mostly on Java, spring, JavaScript, Front end like Angular, JSF, Vaadin etc. His interest in Integration of software’s, SOA and process re-engineering. He runs his own IT consultancy and provided all sort of IT solutions to SME (Small to Medium Enterprises).

MongoDb Aggregate Count Example

Today we will learn about MongoDb’s aggregate function ‘Count,’ but first let’s look at a brief overview of aggregate functions. In SQL or other relational databases, aggregation in the process that records and returns computed results like SUM MIN, MAX, AVG, and COUNT. MongoDb uses a similar concept but in a different fashion – it uses the concept of data processing pipelines.

Documents enter a multi-stage pipeline which transforms them into an aggregated result. Here, ‘Document’ refers to a record, so don’t confuse it if you are new to MongoDb; I will explain when needed.

1. Aggregate Concept

There are several stages in Data Aggregation like filtering, Sorting, grouping etc. , documents transformation that modify the form of the

1.1 Count() Aggregate function

Count is an aggregate function used to count the number of documents, documents per group, or perform a Condition based count. It is is the same as COUNT in SQL or similar languages. Let’s see some examples:

First, we need to perform some basic operations, like create a database, collection, and then insertion of some documents to perform the Count query.

1.2 Create Database

The first step is to create a database. MongoDb uses DATABASE_NAME to create a database. The command will create a new database if it doesn’t exist, otherwise it will return the existing one with the name used. So ‘use’ is the Keyword, and ‘DATABASE_NAME’ can be any name.

Let’s see how many databases there are in the system.

Note: I’m using Compass MongoDb GUI – you can download it from the MongoDb site. I will also show you the shell command to enable a better understanding.

In Compass, you can see the database listed.


There are 3 Databases and 4 collections showing above. Let’s create a new Database. In Shell, we can see the listed database.
Let’s create a Database using the shell




Above we showed that database myFirstDataBase has been created but it’s not showing database in shell. The reason is that we have not inserted any records because it’s an empty database. Lets insert a record but before that let’s see what Compass is showing.


It’s the same – there’s no difference between the shell and Compass. Now insert a document (record) into the database. Do it from the shell, and we will see it in Compass too.




db is the current database
myFirstCol is the Collection name which is automatically created
insert is the MongoDb keyword to insert document
code is a field/attribute
001 is the value of code

I inserted some documents into the database using myFirstCol.


1.3 Documents Count

db.collection.count(query, options)
Where ‘query’ is the Selection criteria and ‘options’ is an extra option for modifying count.


There is another construct



The difference here is that ‘find’ will find all documents and then count, whereas db.collection.count() will just count the documents.

To clarify: ‘find()’ selects documents in a collection that match the query criteria, and returns a cursor to the selected documents . When the find() method “returns documents,” it is actually returning a cursor to the documents. In contrast, db.collection.count() performs the query and counts the documents.

Whereas, db.collection.count() just do the query and count the documents.

Note: Sometimes in a shared cluster environment, db.collection.count() may give you an inaccurate result due to invalid documents being present during abnormal migration or shutdown. This is outside the scope of this document, but we may discuss in in another article. Returning to db.collection.count(query, options) – query is the condition, and options are extra methods modify the count result.


Above shows count operation on collection “Where Grade = “B””, then shows skip(1) and limit(1) optional document attributes.
The Skip process is -> Condition(Where Grade = ‘B’) -> Skip n documents -> Count, and the same for Limit. We can use multiple conditions to get the required result.

1.4 Cursor Count

As explained earlier, count the documents in the cursor by using db.collection.find(query).count(), count is appended in find(). The operation does not perform the query but instead counts the number of results that would be returned by the query.


1.5 Count using Aggregate function

There are more powerful ways to do count, lets see how aggregate helps us


Aggregate is a pipeline, $group is stage. Above query is group by grade. You can do many things like create a condition using $match stage OR if you want to make a complicated query like $Group -> $Match -> $Sort -> $Group and then summarize your result using $Project etc. Its totally dependent on the complexity of your requirements but here it’s out of scope.

2. Summary

Count gives an idea of how it works in MongoDb. We covered two type of count construct: One is db.collection.count() and db.collection.find().count(). We also showed how to do grouping and then count, and a little about aggregate function and its usage. This is just a brief about explanation of count in MongoDb.

This article has been proofread by Mark Adams

(+1 rating, 1 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply

Notify of