Provide support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
Pull request Compare This branch is 2 commits ahead, 805 commits behind spring-projects:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Spring Data MongoDB

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services.

The Spring Data MongoDB aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities. The Spring Data MongoDB project provides integration with the MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB DBCollection and easily writing a Repository style data access layer

Getting Help

For a comprehensive treatmet of all the Spring Data MongoDB features, please refer to the The User Guide

The JavaDocs have extensive comments in them as well.

The home page of Spring Data MongoDB contains links to articles and other resources.

For more detailed questions, use the forum.

If you are new to Spring as well as to Spring Data, look for information about Spring projects.

Quick Start


For those in a hurry:

  • Download the jar through Maven: spring-data-mongodb 1.0.0.BUILD-SNAPSHOT

    spring-maven-snapshot true Springframework Maven SNAPSHOT Repository


MongoTemplate is the central support class for Mongo database operations. It provides

Future plans are to support optional logging and/or exception throwing based on WriteResult return value, common map-reduce operations, GridFS operations. A simple API for partial document updates is also planned.

Easy Data Repository generation

To simplify the creation of Data Repositories a generic Repository interface and default implementation is provided. Furthermore, Spring will automatically create a Repository implementation for you that adds implementations of finder methods you specify on an interface.

The Repository interface is

    public interface Repository<T, ID extends Serializable> { 

      T save(T entity);

      List<T> save(Iterable<? extends T> entities);

      T findById(ID id);

      boolean exists(ID id);

      List<T> findAll();

      Long count();

      void delete(T entity);

      void delete(Iterable<? extends T> entities);

      void deleteAll();

The MongoRepository extends Repository and will in future add more Mongo specific methods.

public interface MongoRepository<T, ID extends Serializable> extends
    Repository<T, ID> {

SimpleMongoRepository is the out of the box implementation of the MongoRepository you can use for basid CRUD operations.

To go beyond basic CRUD, extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions such that they can be easily converted into queries.

For example, given a Person class with first and last name properties, a PersonRepository interface that can query for Person by last name and when the first name matches a regular expression is shown below

public interface PersonRepository extends MongoRepository<Person, Long> {

  List<Person> findByLastname(String lastname);

  List<Person> findByFirstnameLike(String firstname);

You can have Spring automatically generate the implemention as shown below

    <bean id="template" class="">
                    <bean class="com.mongodb.Mongo">
                            <constructor-arg value="localhost" />
                            <constructor-arg value="27017" />
            <constructor-arg value="database" />
            <property name="defaultCollectionName" value="springdata" />

    <bean class="">
            <property name="template" ref="template" />
            <property name="repositoryInterface" value="" />

This will register an object in the container named PersonRepository. You can use it as shown below

 public class MyService {

    PersonRepository repository;

    public void doWork() {


       Person person = new Person();
       person =;

       List<Person> lastNameResults = repository.findByLastname("Gierke");

       List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");


Contributing to Spring Data

Here are some ways for you to get involved in the community:

  • Get involved with the Spring community on the Spring Community Forums. Please help out on the forum by responding to questions and joining the debate.
  • Create JIRA tickets for bugs and new features and comment and vote on the ones that you are interested in.
  • Github is for social coding: if you want to write code, we encourage contributions through pull requests from forks of this repository. If you want to contribute code this way, please reference a JIRA ticket as well covering the specific issue you are addressing.
  • Watch for upcoming articles on Spring by subscribing to

Before we accept a non-trivial patch or pull request we will need you to sign the contributor's agreement. Signing the contributor's agreement does not grant anyone commit rights to the main repository, but it does mean that we can accept your contributions, and you will get an author credit if we do. Active contributors might be asked to join the core team, and given the ability to merge pull requests.