Spring Data MongoDB provides repository-based persistence for Mongo document databases. It uses domain-class annotations to map application objects to documents and repository interfaces that follow the familiar Spring Data programming model. section 4.2
A basic nonreactive setup involves these steps:
- Add the Spring Data MongoDB starter, either directly to the build or through Spring Initializr. Spring Boot autoconfiguration then enables automatic repository support. section 4.2.1
- Run MongoDB locally, commonly on port 27017, or use an embedded Flapdoodle database for development and testing. Embedded data disappears when the application restarts. section 4.2.1
- Configure the production host, port, credentials, and database name when MongoDB is not running locally. section 4.2.1
Repository interfaces
A repository can extend CrudRepository without implementation code: IngredientRepository extends CrudRepository<Ingredient, String>, while OrderRepository extends CrudRepository<TacoOrder, String>. The String identifier reflects automatic ID assignment and differs from the identifier types used in other persistence technologies. section 4.2.3
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-data-mongodb
</artifactId>
</dependency>
Use the starter dependency to enable Spring Data MongoDB and its automatically created repository infrastructure. section 4.2.1

