Comprehensive Guide to MongoDB Connection Configuration in Spring Boot

Nov 22, 2025 · Programming · 10 views · 7.8

Keywords: Spring Boot | MongoDB | Connection Configuration

Abstract: This article provides an in-depth exploration of configuring MongoDB connection parameters in Spring Boot applications, covering host/port settings, URI-based configuration, authentication database setup, and driver compatibility. With detailed code examples and property configurations, developers can master connection techniques for various scenarios, including basic connections, authentication, and version-specific considerations.

Overview of Spring Boot and MongoDB Integration

Spring Boot offers auto-configuration support for MongoDB integration through the Spring Data MongoDB module, simplifying database operations. Before configuring connection parameters, ensure that the necessary dependencies are added to the project. For Gradle projects, include the following in the build.gradle file:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

Once dependencies are configured, MongoDB connection parameters can be defined in the application.properties file.

Basic Connection Configuration Methods

Spring Boot supports configuring MongoDB connections via separate host and port properties. In the application.properties file, set the following properties:

spring.data.mongodb.host=mongoserver
spring.data.mongodb.port=27017
spring.data.mongodb.database=mydatabase

Here, the host property specifies the MongoDB server address, port sets the connection port (defaulting to 27017), and database defines the target database name. These configurations correspond to fields in the MongoProperties class:

private String host;
private int port = DBPort.PORT;
private String uri = "mongodb://localhost/test";
private String database;
private String gridFsDatabase;
private String username;
private char[] password;

URI-Based Unified Configuration

In addition to separate host and port settings, Spring Boot allows configuration via the spring.data.mongodb.uri property. This approach is particularly useful with MongoDB 3.0+ Java drivers, as some versions may not support individual host/port configurations. Example URI configuration:

spring.data.mongodb.uri=mongodb://user:secret@mongo1.example.com:12345/mydatabase

The URI format includes complete connection details such as username, password, host address, port, and database name, offering a more concise configuration method.

Authentication Database Configuration

In production environments, the authentication database in MongoDB might differ from the business database. For instance, when using the official Docker image, users created via MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD environment variables are typically located in the admin database. In such cases, configure the authentication database parameter:

spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=<username>
spring.data.mongodb.password=<password>
spring.data.mongodb.database=<target_database>

This configuration ensures authentication occurs on the correct database while connecting to the target business database.

Data Operations and Annotation Usage

After configuration, use MongoTemplate to perform CRUD operations, aggregation queries, and other database tasks. Spring Data MongoDB provides several annotations to streamline development:

Combined with connection configurations, these annotations provide comprehensive MongoDB data access capabilities for Spring Boot applications.

Configuration Selection Recommendations

When choosing a configuration method, consider the following guidelines:

By selecting the appropriate configuration approach, you can ensure stable connections and efficient data operations between Spring Boot applications and MongoDB.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.