CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > MongoDB Group aggregation in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

MongoDB Group aggregation in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 4 min read ยท 0 Comments
Share: in X
MongoDB $group aggregation in Java

In this post, we will learn the easiest way to use MongoDB $group aggregation in Java. First, we will learn to use it in MongoDB itself then try to use that aggregation pipeline in Java.

Let’s analyze a MongoDB document in a reports collection which is given below:

{
  "_id": {
    "$oid": "6315b348d2d8a2744558f444"
  },
  "headerMessage": "This is testing Header Message.",
  "job": {
    "jobId": "Z0-008",
    "jobName": "2020-RC Sick"
  },
  "reportId": 1,
  "lastUpdatedDateTime": {
    "$date": "2022-09-05T08:35:56.983Z"
  },
  "formDetail": {
    "formId": "6315ad6bd2d8a2744558f441",
    "formTitle": "New Test 1"
  }
}

There are many documents in reports collection. We need to aggregate it so that we can count the total reports per job and also display the job information. The expected output is given below:

{
  "_id": "Z0-008",
  "totalReports": 10,
  "job": {
    "jobId": "Z0-008",
    "jobName": "2020-RC Sick or Vacation"
  }
}

MongoDB $group aggregation example

The syntax of $group aggregation is:


{
  $group:
    {
      _id: <expression>, // Group key
      <field1>: { <accumulator1> : <expression1> },
      ...
    }
 }

Example:

[
  {
    "$group": {
      "_id": "$job.jobId",
      "totalReports": {
        "$sum": 1
      }
    }
  }
]

In the above example:

  • The value $job.jobId is the value by which we want to group report. We are using $ sign that is to use actual value of that field from the original document.
  • The totalReports is output field that holds the accumulated value that is increased by 1 as number of reports found in the list.
  • The operator $sum sums the value by 1 we’ve defined.

The output of the above query will be like below:

{
  "_id": "B-001A",
  "totalReports": 10
}

The $group operator done its job but this is not the output that we’ve expected.

To get the expected output we need to use $first operator.

The $first operator is an accumulator operator in MongoDB aggregation. The $first operator can be used as an array operator but we are not going to discuss it here. We will use $first opeartor as an accumulator operator in the aggregation pipeline in $group stage.

To use $first operator along with $group we can take the reference from the following query:

[
  {
    "$group": {
      "_id": "$job.jobId",
      "totalReports": {
        "$sum": 1
      },
      "job": {
        "$first": "$job"
      }
    }
  }
]

The $first is accumulator operator and $job is the reference object of the job field in the original document. If we don’t reference job field with the help of $ sign then it will display only job as value, not the whole object. Hence, the result of the above query will be the following:

{
  "_id": "Z0-008",
  "totalReports": 10,
  "job": {
    "jobId": "Z0-008",
    "jobName": "2020-RC Sick or Vacation"
  }
}

Let’s see the output of the query without referencing the job object.

Query:

[
  {
    "$group": {
      "_id": "$job.jobId",
      "totalReports": {
        "$sum": 1
      },
      "job": {
        "$first": "job"
      }
    }
  }
]

Output:

{
  "_id": "Z0-008",
  "totalReports": 10,
  "job": "job"
}

That is why, it is very important to understand the use of $ sign.

Implement $group aggregation in Java

To implement $group aggregation in Java, we must have a MongoDB Java driver in the classpath. By adding this driver to the classpath we can easily use its classes and interfaces that are required to connect to the MongoDB database and execute queries.

Let’s create an aggregation pipeline and add multiple stages one by one into the pipeline.

Creating aggregation pipeline list.

List<Bson> aggregationPipelines = new ArrayList<>();

Create a group stage of the aggregation pipeline by implementing the query we discussed earlier.

Bson groupStage = Aggregates.group("$job.jobId", Accumulators.sum("totalReports", 1),
				Accumulators.first("job", "$job"));

Now, add this group stage to our aggregationPipelines list.

aggregationPipelines.add(groupStage);

We can use this list of aggregationPipelines to execute aggregation using MongoCollection.

If you don’t know how to create a MongoCollection object then you can read our earlier post MongoCollection in Spring Boot which is based on Spring Boot but this will help if you are building a standalone application.

The final code will look like the below:

// Import packages
import org.bson.conversions.Bson;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;

//Create multiple stages like match, group, sort and etc then add these stages to aggregationPipelines which is simply a Bson objects

List<Bson> aggregationPipelines = new ArrayList<>();

Bson groupStage = Aggregates.group("$job.jobId", Accumulators.sum("totalReports", 1),
				Accumulators.first("job", "$job"));

// aggregationPipelines.add(matchStage); // add match stage if you have
aggregationPipelines.add(groupStage);
//aggregationPipelines.add(sortStage); // add sort stage if you have

// Execute aggregation pipeline with MongoCollection
reportDataCollection.aggregate(aggregationPipelines, MyOutput.class).into(outputList);

After executing an aggregate method from the MongoCollection interface it will add results to outputList object. Then we can use that object for further processing.

Conclusion

In this post, we learned to implement the MongoDB aggregation pipeline along with $group in Java.

Related Posts:

  • Update MongoDB Document
  • Delete in MongoDB
  • MongoDB bulkWrite in Java: A Comprehensive Guide
  • What is MongoDB?
  • Array Operator in MongoDB
  • How To Print Git Commit Message In Jenkins Pipeline
Tags:javamongodb
Was this article helpful?
โ† Previous ArticleArray Operator in MongoDB
Next Article โ†’Java Bytecode

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap