The spring-boot-starter-data-mongodb dependency is the single entry you add to a Spring Boot project to work with MongoDB through Spring Data. It pulls in Spring Data MongoDB and the MongoDB Java driver, then auto-configures a MongoClient, a MongoTemplate and repository support from your application.properties, so you write entities and repository interfaces instead of connection code.
It is a blocking (synchronous) starter. For non-blocking access with Project Reactor, use spring-boot-starter-data-mongodb-reactive instead.
A Spring Boot starter is a curated set of dependencies plus auto-configuration, with no code of its own. This one gives you:
| Piece | Purpose |
|---|---|
| Spring Data MongoDB | Repositories, MongoTemplate, mapping annotations, query derivation |
| MongoDB Java driver (sync) | Wire protocol, connection pooling, BSON codecs |
| Spring Boot auto-configuration | Creates MongoClient, MongoDatabaseFactory, MongoTemplate, repositories |
You do not set a version. The Boot dependency management (inherited from spring-boot-starter-parent or imported as a BOM) picks versions that are tested together.
In Gradle:
The simplest configuration is a connection string:
You can also set individual properties such as host, port and database name. Credentials, replica sets, TLS and retryWrites belong in the connection string, which is the form the MongoDB driver documents. Spring Boot 4 reorganised some MongoDB properties into a driver-level spring.mongodb.* namespace, so check the configuration reference for your Boot version if a property is not recognised.
Do not hard-code credentials. Inject the URI from an environment variable or a secrets manager:
Entities are plain classes (records work well for immutable data) annotated with @Document. The field annotated @Id becomes the _id of the document.
Automatic index creation from @Indexed is off by default in recent Spring Data MongoDB versions. Enable it with spring.data.mongodb.auto-index-creation=true only where that is acceptable, and manage production indexes through migrations or your deployment tooling.
Extend MongoRepository and Spring Data generates the implementation at startup. Method names are parsed into queries.
A service can then use it directly:
Constructor injection, as above, is preferred over field injection because the class stays easy to test.
Repositories cover common cases. For updates of single fields, aggregations or dynamic criteria, inject MongoTemplate:
An atomic partial update like this avoids reading and rewriting the whole document.
@DataMongoTest starts a slice of the context with only MongoDB components.MongoTransactionManager bean, and you then use @Transactional.No. It uses the synchronous driver. Add spring-boot-starter-data-mongodb-reactive for ReactiveMongoRepository and ReactiveMongoTemplate.
No, when you use the Spring Boot parent or BOM. The version is managed for you and matches the Spring Boot release.
Automatic index creation is disabled by default. Either enable spring.data.mongodb.auto-index-creation for development or create indexes explicitly.
Yes. Use the mongodb+srv:// connection string Atlas provides as spring.data.mongodb.uri.
spring-boot-starter-data-mongodb bundles Spring Data MongoDB and the MongoDB driver and auto-configures the client, template and repositories from a connection string. Annotate entities with @Document, declare repository interfaces for derived queries, and use MongoTemplate for partial updates and aggregations. Keep credentials in the environment, create production indexes deliberately, and test against a real MongoDB instance.