Overview of Spring Data MongoDB


The Spring Data umbrella project aims at providing support for new data access technologies. It hosts many sub-projects and one of them is dedicated to MongoDB. Spring Data MongoDB comes with tons of features for the Java developers working with MongoDB: database and collection management, lightweight object-document mapping, and dynamic repositories are some of these features. Let’s take a tour!

Working on DBObjects with the MongoTemplate

The MongoTemplate is easy to set up, it justs needs a Mongo instance (i.e. connection to MongoDB) and the name of the database to work with:

Note we use the MongoOperations interface. Working against interfaces is a best practice and can make your life easier if you want to mock your Mongo template with JDK proxies.

The Mongo template provides methods to manipulate collections:

It lets you also work with plain DBObject (the document interface in the MongoDB Java driver):

Neat, but nothing much new compared to the basic Java driver so far. This is where it comes to work with domain objects that the Mongo template really shines.

Object-Document mapping

Using a document datastore doesn’t mean your application doesn’t have a fixed, rich domain model. Working with a set a domain classes makes you more productive than strictly working with DBOjects, which are – let’s face it – hard to manipulate.

Spring’s MongoTemplate offers a powerful object-document mapping engine. Such a tool is convenient for validation, type checking, and associations.

Spring’s object-document mapper is as simple as this to use:

The Mongo template stores the Contact object in a contact collection. If we use the Mongo shell to see the content of the collection:

You don’t need to provide any mapping metadata, but you can if you want to:

Once there are documents in your collections, you usually want to retrieve them through some query API. Let’s see what the Mongo template offers.

Query DSL

Spring Data MongoDB provides a fluent query API. The Mongo template has a find method that accepts a Query instance. Such a Query is usually created with static factory methods:


This query API supports all the MongoDB query features (operators like greater than, and, or, and so on, sort, and pagination as well).

The Mongo template has a great API you can use everywhere in your applications, but you can also choose to have a dedicated access layer. Spring Data MongoDB can also help by providing ready-to-use, dynamic repositories.

Dynamic repositories

A data access layer can be useful to abstract the underlying data access technology of an application. Upper layers – usually a business layer – don’t know anything about this data access technology. Such an abstraction is useful if the application switches from one framework to another or if one mocks the data access layer to unit test the business layer more easily.

With Spring Data MongoDB, one can easily expose CRUD repositories. The first step is to declare a repository interface for an entity class:

The second step is to tell Spring Data MongoDB where to find your repository interfaces:

Note you need to provide the framework with a Mongo instance and a Mongo template. You don’t need to provide any implementation for the repository: Spring Data MongoDB implements the behavior dynamically. You just have to look up the repo from the Spring context and use it:

By extending MongoRepository, the repository provides all the CRUD operations (with bonuses like sorting and paging). By following conventions, you can also provide your own methods, the repository will implements them on the fly:

Thanks to the name of the method, Spring Data MongoDB understands the criteria is on the firstname property.

This dynamic repository support is sophisticated: one can « blend » its own implementation with the dynamic ones, add parameters for sorting or paging, provides JSON-based queries with annotations, and so on. Note Spring Data provides the exact same support for JPA.


The Spring Data MongoDB provides a comprehensive, yet progressive support for Java-based MongoDB applications. MongoDB’s Java driver works great, but can frustrate people used to working with strongly-typed domain models.

We scratch
ed only the surface of Spring Data MongoDB, don’t hesitate to check its reference documentation for the JPA integration (polyglot persistence), the listener support (callbacks around the lifecycle of entities), or its geo-spatial integration. Check out also the brand-new Spring Data REST project if you want to expose your MongoDB documents as REST resources thanks to the dynamic repositories.

Source code

Partagez cet article.

A propos de l'auteur

Ajouter un commentaire