Last updated: 2024-10-04
Setting up a Spring Boot application with MongoDB
MongoDB now available in the Free plan! Create your next Spring Boot app with custom database schema in minutes - following best practices and with the full setup for Spring Data MongoDB.
Discover more
Spring Boot provides excellent support for applications accessing MongoDB as a database. The following preparations are necessary to create our own application using Spring Data MongoDB.
Configuration
As a document-oriented database, MongoDB will not be connected to our application using Hibernate as an ORM layer. Instead, we add spring-boot-starter-data-mongodb
as a dependency to our app, providing a similar approach to accessing MongoDB using POJOs and repositories.
Required Maven dependency
In Gradle the same dependencies looks like implementation 'org.springframework.boot:spring-boot-starter-data-mongodb
. We don't need a version, as this is automatically provided by Spring Boot Dependency Management.
With this in place, we can extend our application.yml
or application.properties
with the uri. If the database does not exist yet, MongoDB will create it. With setting auto-index-creation
to true
, unique indexes are added during startup of our app if they are annotated in our documents with @Indexed(unique = true)
. This isn't required when working with Mongock.
Only the uri is required for a successful database connection
In our configuration class, @EnableMongoRepositories
should be set. This will search for classes in the specified package that extend MongoRepository
- an example of this will be given in a moment. By providing the ValidatingMongoEventListener
all documents are validated before persisting, and we can extend their fields with javax.validation
constraints like @NotNull
.
First version of our config
Furthermore we want to enable transaction support for our application. This allows us, for example, to mark the methods of our services as @Transactional
. Please note that this requires our MongoDB instance to be initialized as replica set to support this feature.
Adding transaction support to our config
Example Document and Repository
With these preparations, we can already define a simple document and its associated repository. For our example we use a primary key of type String, which is automatically generated by MongoDB as ObjectID if we do not provide it. To use our own sequence as a primary key, there are instructions on how to do so in this article.
Our first POJO, mapped to a collection named "customer"
We add our repository by extending MongoRepository
. It works in the same way as known from Spring Data and can be extended with custom queries.
Basic Repository for MongoDB
With this, all preparations are done to connect our Spring Boot application with MongoDB. In Bootify's Free plan, we can generate the application with our custom database schema without registration. The runnable source code of our application is then directly available for download.
Start Project
No registration required