Y NOT — Integrate Spring Boot App with MongoDB And Retrieve Documents Based on Filters, Aggregations
In this article, I would like to show how spring boot application can be integrated with MongoDB (Schema-Less, Document Based NoSQL Database). To know more about MongoDB, refer here.
I have created a simple Spring Boot application with UserInfo as the entity. This adheres to the document containing user information. Moreover UserInfo also has some embedded documents like Name, DOB, Location etc.
To start with, created a spring boot application with below maven dependencies.
Mongo DB Configuration:
In application.yml file, configurations to connect to Mongo DB server are added. This includes details like mongo server host, port, username, password, database that we are trying to access. To know more about how to set up basic authentication and authorization on MongoDB server, refer here
UserInfo Entity is tagged with @Document annotation followed by collection name. Using Lombok utility, getters/setters and constructors are defined with respective annotations.
Filter Based on Last Name — GET API
UserInfoRepository will extend MongoRepository to make use of available retrieval methods like findAll(). We can also create custom methods and define filter logic using @Query and ?0 indicates first input argument. Similar ?1 will be the second and ?2 will be the thirds input argument passed on to this method invocation.
Filter Based On Comparison Operators — GET API
To filter documents based on age, below comparison operators can be used. Moreover, If we do not need all the attributes of a document in our response, we can specific inclusion or exclusion fields that defines our output response. In below snapshot, we have only included name, dob and gender attributes.
Filter Based on Logical Operators — GET API
To filter documents based on multiple conditions, we make use of logical operators like $and, $or, $nor etc.
Aggregation: Extract Location Wise Female Count — GET API
MongoDB provides aggregation framework where we can configure multiple stages in a sequential order to form a pipeline. To extract the location wise female count, below steps are followed.
Stage 1: Filter records with Gender as female
Stage 2: Group records based on Location and extract the count on each location
Stage 3: Sort the response based on the descender order (Location with maximum female count comes first in the output response)
Aggregation: Extract Location Wise Male Count, Female Count, Total Population— GET API
Stage 1: Group records based on two fields (location and gender) and extract the count. This will give us two records for each state (one having male count and other having female count)
Stage 2: Project records onto fields applicable fields (male count if gender is male, female count if gender is female).
Stage 3: Group again based on location alone and extract the sum against male count, female count and total population. This will result in one record per state holding male, female and total population count on that state.
Stage 4: Sort records based on ascending order of the state.
Source Code for this project is available in link.
Moreover, persons.json file has been uploaded on src/main/resources/ that consists of 5000 documents. This has been bulk imported to MongoDB for validating these use cases. To know more about bulk import, please refer my MongoDB article earlier.
REST API Endpoints:
Get All Users:
Filter Based On Comparison Operators:
Filter Based on Logical Operators:
Aggregation Based Data Extract:
Hope you enjoyed this article!