Home » Software Development » MongoDB » MongoDB Aggregation Example

About Yatin

Yatin
The author is graduated in Electronics & Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java/J2EE technologies platform and novel UI technologies.

MongoDB Aggregation Example

Hello readers, aggregation in Mongo database is an operation that processes the multiple documents and returns a single computed result. In this tutorial, we will learn how to use the different aggregation operations in the Mongo database.

1. Introduction

If you have installed the MongoDB application (version 3.6) on Windows or Ubuntu operating system and you wish to learn the regular expressions then follow the below steps. It is very simple, but before moving further let’s take a look at the Mongo database and its characteristics.

1.1 What is MongoDB?

  • MongoDB is a high-performance NoSQL database where each database has collections which in turn has documents. Each document has a different number of fields, size, content, and is stored in a JSON-like format (i.e. Binary JSON (BSN))
  • The documents in MongoDB don’t need to have a schema defined beforehand. Instead, the fields (i.e. records) can be created on the go
  • Data model available within the MongoDB allows developers to represent the hierarchical relationships, store arrays, and other more complex structures easily
  • This NoSQL solution often comes with embedding, auto-sharding, and onboard replication for better scalability and high availability

1.1.1 Why MongoDB?

  • As a NoSQL type database, MongoDB stores the data in the form of a document. Thus, MongoDB offers more flexibility
  • This database supports search by field-name, range queries, and the regular expressions. It often provides queries to return the particular fields inside the documents
  • MongoDB offers indexes to improve the search performance within the NoSQL database
  • To offer horizontal scalability, MongoDB uses sharding by splitting the data across the many MongoDB occurrences
  • Replication: MongoDB can give high availability with the replica sets

2. MongoDB Aggregation Example

In this tutorial, we will learn what “aggregation” is and how to handle the different “aggregation operations” in the Mongo database.

2.1 Aggregation in the Mongo database

In the Mongo universe, the aggregate() method groups the documents of a collection to provide a total sum, average, minimum, maximum etc. of the grouped documents. In other words, this method calculates the gross values for the documents in a collection. The aggregate() method has the following prototype form:

Mongo database ‘aggregate()’ method Syntax

> db.collection_name.aggregate(pipeline_operation, options)

Where:

  • The collection_name is the name of the collection on which the aggregate function is applied
  • A pipeline_operation is a required array argument which performs a sequence of aggregation options. Each option transforms the Mongo document and gives a final computed result
  • The options are the optional input argument that is passed to the aggregation function
Fig. 1: Aggregation/Pipeline operation in Mongo database

Fig. 1: Aggregation/Pipeline operation in Mongo database

2.1.1 Different Pipeline operators

Below table lists the different pipeline operators that are commonly used in the Mongo database.

SQLMongoDBDescriptionExample Query
SELECT$projectThis function passes the existing fields from the Mongo document or the newly evaluated fields to the next option in the pipeline. In this, developers use ‘1’ or ‘true’ if they want to include the Field and ‘0’ or ‘false’ if they want to exclude a field.db.employee.aggregate([ {"$project": { "_id" : 0, "emp_fname" : 1, "emp_dept" : 1, "emp_band" : 1, "emp_specs" : 1 }} ])
WHERE$matchThis function filters the documents of a collection that matches the specific criteria and pass only the matched documents to the next pipeline option.db.employee.aggregate([ {"$match": { "emp_dept": "Finance" }} ])
LIMIT$limitThis function bound the first ‘n‘ unmodified documents and passes them to the next option in the pipeline.db.employee.aggregate([ {"$match": { "emp_dept": "Technology" }}, {"$limit": 1} ])
GROUPBY$groupThis function groups the documents of a Mongo collection by specific criteria and pass them to the next option of the aggregation pipeline.db.employee.aggregate([ {"$group": {"_id": {"emp_dept" : "$emp_dept"}, "No_of_Times": {"$sum": 1}}} ])
ORDERBY$sortThis function re-orders the documents of a Mongo collection either in the ascending or the descending order.db.employee.aggregate([ {"$match": { "emp_dept": "HR" }}, {"$sort" : {"emp_fname": 1}} ])
JOIN$lookupThis function performs a left-outer join with another collection in the same Mongo database.db.emp_city.aggregate([ {$lookup: {from: "employee", localField: "emp_country_id", foreignField: "_id", as: "Country" }} ])
$outThis function writes the computed result to a specific collection in the Mongo database. This operator must be the last option in the aggregation pipeline.db.employee.aggregate([ {"$match": { "emp_dept": "HR" }}, {"$sort" : {"emp_fname": 1}}, {"$out" : "sorted_emp"} ])
$unwindThis function deconstructs an array field from the inputted document to output a document for each element.db.employee.aggregate([ {"$match": { "emp_fname": "April" }}, {"$unwind": "$emp_specs"} ])

2.1.2 Different Expressions used by the Aggregation function

Below table lists the different expressions that are used by the aggregation function in the Mongo database.

ExpressionDescription
1$sumAmount the defined values of all the documents in a collection.
2$avgCalculate the average values of all the documents in a collection.
3$minReturn the minimum value.
4$maxReturn the maximum value.

2.2 Practical usage

Let’s understand the implementation of this method with the help of the sample snippets.

2.2.1 Start MongoDB

Start a standalone mongod instance as shown below.

Fig. 2: Start Mongo instance

Fig. 2: Start Mongo instance

2.2.2 Connect to the Mongo Instance

Connect with the mongo shell to make a connection with the MongoDB instance on the port 27017 as shown below.

Fig. 3: Connect to Mongo database

Fig. 3: Connect to Mongo database

2.2.3 Create Mongo database and collection

To begin with the implementation, we will need to create a sample database and collection. The below script creates a database called office with a collection of employee. Open the Mongo terminal and execute the script.

Database & Collection creation script

> use office

> db.employee.insertMany( [
	{ _id: 1, "emp_fname" : "Jason", "emp_dept" : "Technology", "emp_band" : "C1", "emp_specs": [ "Java", "AngularJs", "MongoDb" ] },
	{ _id: 2, "emp_fname" : "Charlotte", "emp_dept" : "Finance", "emp_band" : "C0", "emp_specs": [ "Accounting" ] },
	{ _id: 3, "emp_fname" : "Daniel", "emp_dept" : "Technology", "emp_band" : "C2", "emp_specs": [ "Java", "AngularJs" ] },
	{ _id: 4, "emp_fname" : "April", "emp_dept" : "Technology", "emp_band" : "C1", "emp_specs": [ "AngularJs", "MongoDb" ] },
	{ _id: 5, "emp_fname" : "Ed", "emp_dept" : "Finance", "emp_band" : "C1", "emp_specs": [ "Accounting", "Payroll" ] },
	{ _id: 6, "emp_fname" : "Susan", "emp_dept" : "HR", "emp_band" : "C0", "emp_specs": [ "Personality Development", "Employee Concern" ] },
	{ _id: 7, "emp_fname" : "Marie", "emp_dept" : "HR", "emp_band" : "C2", "emp_specs": [ "Employee Safety" ] },
	{ _id: 8, "emp_fname" : "Jeff", "emp_dept" : "COO", "emp_band" : "C5", "emp_specs": [ "Delivery Leader", "Employee Safety" ] },
	{ _id: 9, "emp_fname" : "John", "emp_dept" : "CEO", "emp_band" : "C8", "emp_specs": [ "Global Delivery Leader" ] }
] )

The script gives the below output.

Fig. 4: Database & Collection creation

Fig. 4: Database & Collection creation

2.2.4 Check Mongo database and collection

If the script works well, the database and the collection will be shown in the Mongo Workbench. Using the db.collection_name.find() or the db.collection_name.find().pretty() command the documents of a collection will be shown as below.

Fig. 5: Mongo database & collection

Fig. 5: Mongo database & collection

2.2.5 Implementation of Aggregation method

Now, go back to the Mongo shell and let’s understand the practical implementation of the ‘aggregation’ function in the Mongo world. The following Mongo database command can be used.

Query 1

> db.employee.aggregate([ {"$match": { "emp_dept": "Technology" }}, {"$group": {"_id": {"emp_dept" : "$emp_dept"}, "No_of_Times": {"$sum": 1}}} ])

As shown in Fig. 6, the Query 1 snippet will start looking into the employee collection for the documents where emp_dept equals to “Technology” and group the matching documents by the emp_dept field and calculates the no. of times each group appears.

Fig. 6: Mongo database 'Aggregation' function

Fig. 6: Mongo database ‘Aggregation’ function

Let’s say developers want to group the documents of a collection on the basis of employee department and display the total number of employees present under that department. The following Mongo database command can be used.

Query 2

> db.employee.aggregate([ {"$group": {"_id": {"emp_dept" : "$emp_dept"}, "Total_count_of_employees": {"$sum": 1}}} ])

As shown in Fig. 7, the Query 2 aggregate function will group the collection data based on the departments and display the total number of employees.

Fig. 7: Mongo database 'Aggregation' function with '$group' and '$sum' Operators

Fig. 7: Mongo database ‘Aggregation’ function with ‘$group’ and ‘$sum’ Operators

That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, we learned about the different aggregation operations in the Mongo database. Developers can download the sample Mongo shell commands in the Downloads section.

4. Download the Eclipse Project

This was an example of implementing and using the aggregation operations in the Mongo database.

Download
You can download the full source code of this example here: Aggregation Operations
(No Ratings Yet)
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

avatar
  Subscribe  
Notify of