/spring-data-mongodb

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.

Primary LanguageJava

Spring Data MongoDB Spring Data MongoDB

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 project aims to provide a familiar and consistent Spring-based programming model 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 treatment of all the Spring Data MongoDB features, please refer to:

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

Quick Start

Maven configuration

Add the Maven dependency:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>${version}.RELEASE</version>
</dependency>

If you'd rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>${version}.BUILD-SNAPSHOT</version>
</dependency>

<repository>
  <id>spring-libs-snapshot</id>
  <name>Spring Snapshot Repository</name>
  <url>http://repo.spring.io/libs-snapshot</url>
</repository>

MongoTemplate

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

  • Basic POJO mapping support to and from BSON
  • Convenience methods to interact with the store (insert object, update objects) and MongoDB specific ones (geo-spatial operations, upserts, map-reduce etc.)
  • Connection affinity callback
  • Exception translation into Spring's technology agnostic DAO exception hierarchy.

Spring Data repositories

To simplify the creation of data repositories Spring Data MongoDB provides a generic repository programming model. It will automatically create a repository proxy for you that adds implementations of finder methods you specify on an interface.

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 like expression is shown below:

public interface PersonRepository extends CrudRepository<Person, Long> {

  List<Person> findByLastname(String lastname);

  List<Person> findByFirstnameLike(String firstname);
}

The queries issued on execution will be derived from the method name. Extending CrudRepository causes CRUD methods being pulled into the interface so that you can easily save and find single entities and collections of them.

You can have Spring automatically create a proxy for the interface by using the following JavaConfig:

@Configuration
@EnableMongoRepositories
class ApplicationConfig extends AbstractMongoConfiguration {

  @Override
  public MongoClient mongoClient() throws Exception {
    return new MongoClient();
  }

  @Override
  protected String getDatabaseName() {
    return "springdata";
  }
}

This sets up a connection to a local MongoDB instance and enables the detection of Spring Data repositories (through @EnableMongoRepositories). The same configuration would look like this in XML:

<bean id="template" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg>
    <bean class="com.mongodb.MongoClient">
       <constructor-arg value="localhost" />
       <constructor-arg value="27017" />
    </bean>
  </constructor-arg>
  <constructor-arg value="database" />
</bean>

<mongo:repositories base-package="com.acme.repository" />

This will find the repository interface and register a proxy object in the container. You can use it as shown below:

@Service
public class MyService {

  private final PersonRepository repository;

  @Autowired
  public MyService(PersonRepository repository) {
    this.repository = repository;
  }

  public void doWork() {

     repository.deleteAll();

     Person person = new Person();
     person.setFirstname("Oliver");
     person.setLastname("Gierke");
     person = repository.save(person);

     List<Person> lastNameResults = repository.findByLastname("Gierke");
     List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");
 }
}

MongoDB 4.0 Transactions

As of version 4 MongoDB supports Transactions. Transactions are built on top of ClientSessions and therefore require an active session.

MongoTransactionManager is the gateway to the well known Spring transaction support. It allows applications to use managed transaction features of Spring. The MongoTransactionManager binds a ClientSession to the thread. MongoTemplate automatically detects those and operates on them accordingly.

@Configuration
static class Config extends AbstractMongoConfiguration {

	@Bean
	MongoTransactionManager transactionManager(MongoDbFactory dbFactory) {
		return new MongoTransactionManager(dbFactory);
	}

	// ...
}

@Component
public class StateService {

	@Transactional
	void someBusinessFunction(Step step) {

		template.insert(step);

		process(step);

		template.update(Step.class).apply(Update.set("state", // ...
	};
});

Contributing to Spring Data

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

  • Get involved with the Spring community on Stackoverflow and help out on the spring-data-mongodb tag 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 spring.io.

Before we accept a non-trivial patch or pull request we will need you to sign the Contributor License 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. If you forget to do so, you'll be reminded when you submit a pull request. Active contributors might be asked to join the core team, and given the ability to merge pull requests.