This project demonstrates an end-to-end Spring Boot and MongoDB integration using MongoTemplate, repository interfaces, and advanced query techniques.
It manages Trucks and Lease Contracts with various MongoDB features — CRUD, filtering, searching, sorting, pagination, projection, aggregation, and transaction examples.
This Spring Boot application connects to a local MongoDB database (truck_lease_service_db) and exposes REST APIs to manage trucks and lease contracts.
It uses MongoTemplate for dynamic query building and aggregation pipelines, and MongoTransactionManager to enable multi-document transactions.
- Java 21
- Spring Boot 3.5.7
- Maven 3.9+
- MongoDB running locally (default port 27017)
- Postman
-
Start MongoDB locally.
-
To import sample MongoDB data, refer to the instructions in the how-to-import-sample-data.txt file located in the /resources folder of the project.
-
Clone this project:
git clone https://github.com/sirajchaudhary/springboot-mongodb-example.git cd springboot-mongodb-example -
Run the application:
mvn spring-boot:run
-
Import Postman collection: Import the file
postman_collection.jsonlocated in the root folder. -
Test APIs one by one from Postman.
| API Feature | Method | Endpoint | Example Input | Example Behavior |
|---|---|---|---|---|
| Create Truck | POST | /api/trucks | JSON Body | Add a new truck |
| Get All Trucks | GET | /api/trucks | None | Returns list of trucks |
| Get Truck by ID | GET | /api/trucks/{id} | Path ID | Fetch specific truck |
| Delete Truck | DELETE | /api/trucks/{id} | Path ID | Delete truck record |
| Filter Available by City | GET | /api/trucks/available?city=Hyderabad | Query Param | Returns available trucks in Hyderabad |
| Search Trucks | GET | /api/trucks/search?keyword=Refrigerated | Query Param | Search by truck number or type |
| Sort Trucks | GET | /api/trucks/sort?desc=true | Query Param | Sorts by capacity descending |
| Pagination | GET | /api/trucks/page?page=1&size=5 | Query Param | Returns first 5 trucks |
| Projection | GET | /api/trucks/projected | None | Returns selected truck fields |
| Filter by Capacity | GET | /api/trucks/capacity?minTons=25 | Query Param | Trucks with capacity > 25 tons |
| Partial Update City | PATCH | /api/trucks/{truckNumber}/city | Path + Query | Update city of a truck |
| Partial Update Availability | PATCH | /api/trucks/{truckNumber}/availability | Path + Query | Update availability flag |
| Group by City | GET | /api/trucks/groupByCity | None | Aggregates total trucks and capacity per city |
| API Feature | Method | Endpoint | Example Input | Example Behavior |
|---|---|---|---|---|
| Create Lease Contract | POST | /api/contracts | JSON Body | Add a new lease contract |
| Get All Lease Contracts | GET | /api/contracts | None | Fetch all contracts |
| Get Lease Contract by ID | GET | /api/contracts/{id} | Path ID | Fetch single contract |
| Update Lease Contract | PUT | /api/contracts/{id} | JSON Body | Replace full document |
| Delete Lease Contract | DELETE | /api/contracts/{id} | Path ID | Remove contract |
| Filter by Status | GET | /api/contracts/status?status=ACTIVE | Query Param | Filter contracts by status |
| Search Contracts | GET | /api/contracts/search?keyword=Bangalore | Query Param | Search by lessee name or destination |
| Partial Update Status | PATCH | /api/contracts/{id}/status?status=COMPLETED | Path + Query | Update contract status only |
| Partial Update Amount | PATCH | /api/contracts/{id}/amount?amount=90000 | Path + Query | Update only lease amount |
| Aggregation | GET | /api/contracts/totalLeaseByOriginCity | None | Sum lease amount grouped by city |
| Transaction | POST | /api/contracts/{id}/activate | Path ID | Activate a lease via transaction |
| # | Feature | Example Method |
|---|---|---|
| 1 | CRUD | createTruck, createLeaseContract |
| 2 | Filter | findAvailableTrucks, findByStatus |
| 3 | Search (Regex) | searchTrucks, searchLeaseContracts |
| 4 | Sort | sortByCapacity |
| 5 | Pagination | paginateTrucks |
| 6 | Projection | projectedTrucks |
| 7 | Expression (gt) | trucksAboveCapacity |
| 8 | Grouping / Aggregation | groupByCity, totalLeaseByOriginCity |
| 9 | Partial Update (set) | updateTruckCity, updateAvailability, updateLeaseAmount |
| 10 | Transaction | activateLeaseContract |
public List<Truck> searchTrucks(String keyword) {
Query query = new Query(new Criteria().orOperator(
Criteria.where("truckNumber").regex(keyword, "i"),
Criteria.where("type").regex(keyword, "i")
));
return mongoTemplate.find(query, Truck.class);
}This uses Criteria.orOperator() with case-insensitive regex for flexible searching.
public List<Truck> sortByCapacity(boolean desc) {
Sort.Direction direction = desc ? Sort.Direction.DESC : Sort.Direction.ASC;
Query query = new Query().with(Sort.by(direction, "capacityTons"));
return mongoTemplate.find(query, Truck.class);
}Implemented using Sort.by() with ascending or descending direction dynamically.
public List<Truck> findAvailableTrucks(String city) {
Query query = new Query(Criteria.where("available").is(true)
.and("currentCity").is(city));
return mongoTemplate.find(query, Truck.class);
}Demonstrates filter chaining using multiple Criteria fields.
public List<Truck> paginateTrucks(int page, int size) {
Query query = new Query().skip((long) (page - 1) * size).limit(size);
return mongoTemplate.find(query, Truck.class);
}Implements manual pagination using skip() and limit().
public List<Truck> projectedTrucks() {
Query query = new Query();
query.fields().include("truckNumber").include("type").include("available");
return mongoTemplate.find(query, Truck.class);
}Shows how to include only specific fields using projection.
public List<?> groupByCity() {
Aggregation agg = Aggregation.newAggregation(
Aggregation.group("currentCity")
.count().as("truckCount")
.sum("capacityTons").as("totalCapacity")
);
return mongoTemplate.aggregate(agg, "trucks", Object.class).getMappedResults();
}Performs grouping and summarization via the aggregation pipeline.
@Transactional
public void activateLeaseContract(String id) {
LeaseContract contract = leaseContractRepository.findById(id).orElseThrow();
contract.setStatus("ACTIVE");
leaseContractRepository.save(contract);
}Enabled by MongoTransactionManager for safe updates across documents.
springboot-mongodb-example/
│
├── src/main/java/com/example/
│ ├── controller/
│ │ ├── TruckController.java
│ │ └── LeaseContractController.java
│ ├── service/
│ │ ├── TruckService.java
│ │ └── LeaseContractService.java
│ ├── model/
│ │ ├── Truck.java
│ │ └── LeaseContract.java
│ └── config/
│ └── MongoConfig.java
│
├── sample-mongodb-data/
│ ├── trucks.json
│ └── leaseContracts.json
│
├── postman_collection.json
├── pom.xml
├── README.md
└── application.properties
This project demonstrates how to implement advanced MongoDB operations in a clean, layered Spring Boot architecture — showcasing powerful usage of MongoTemplate, Criteria, and transactions.
It can serve as a boilerplate for any logistics or rental management microservice.
# Use Eclipse Temurin JDK as base image
FROM eclipse-temurin:21-jdk
# Set the working directory inside the container
WORKDIR /app
# Copy the jar file built by Maven into the container
COPY target/*.jar app.jar
# Expose the port your Spring Boot app runs on (default 8080)
EXPOSE 8080
# Run the jar file
ENTRYPOINT ["java", "-jar", "app.jar"]Update your application.properties (or application.yml) so that MongoDB is accessed from the host machine, not inside the container.
Replace:
#spring.data.mongodb.uri=mongodb://localhost:27017/truck_lease_service_dbWith:
spring.data.mongodb.uri=mongodb://10.107.6.110:27017/truck_lease_service_dbHere
10.107.6.110is your system's IP address.
You can find it using the command:ipconfig
Run the following Maven command to build your JAR:
mvn clean installBy default, MongoDB on Windows binds only to 127.0.0.1.
Edit your MongoDB configuration file:
C:\Program Files\MongoDB\Server\<version>\bin\mongod.cfg
Find:
net:
bindIp: 127.0.0.1
port: 27017Change to:
net:
port: 27017
bindIp: 0.0.0.0Then restart MongoDB:
net stop MongoDB
net start MongoDBOpen PowerShell as Administrator and run:
New-NetFirewallRule -DisplayName "Allow MongoDB 27017" -Direction Inbound -Protocol TCP -LocalPort 27017 -Action AllowBuild the Podman image:
podman build -t springboot-mongodb-example .Run the container:
podman run -d --name springboot-mongodb-example-container -p 8080:8080 springboot-mongodb-exampleEnter the container shell:
podman exec -it springboot-mongodb-example-container bashInstall netcat:
apt-get update && apt-get install -y netcat
apt-get install -y netcat-openbsdTest MongoDB connection:
nc -zv 10.107.6.110 27017If you see:
Connection to 10.107.6.110 27017 port [tcp/*] succeeded!
MongoDB is reachable.
Or check via Spring Boot logs:
podman logs -f springboot-mongodb-example-containerIf you see:
Connected to MongoDB at mongodb://10.107.6.110:27017
You’re good to go — MongoDB is connected successfully.
Install curl (if not already installed):
apt-get update && apt-get install -y curlTest your API:
curl http://localhost:8080/api/trucksYou can also use Postman to test the same endpoint.
Free Software, by Siraj Chaudhary