Spring Data Mongodb Reference
Spring Data Mongodb Reference
1.4.2.RELEASE
Mark Pollack, Thomas Risberg, Oliver Gierke, Costin Leau, Jon Brisbin, Thomas Darimont, Christoph Strobl
Copies of this document may be made for your own use and for distribution to others, provided that you do not charge any fee
for such copies and further provided that each copy contains this Copyright Notice, whether distributed in print or electronically.
Spring Data MongoDB - Reference Documentation
Table of Contents
Preface ...................................................................................................................................... v
I. Introduction ............................................................................................................................. 1
1. Requirements ................................................................................................................. 2
2. Additional Help Resources .............................................................................................. 3
2.1. Support ............................................................................................................... 3
Community Forum .............................................................................................. 3
Professional Support .......................................................................................... 3
2.2. Following Development ........................................................................................ 3
3. Working with Spring Data Repositories ............................................................................ 4
3.1. Core concepts ..................................................................................................... 4
3.2. Query methods ................................................................................................... 5
Defining repository interfaces .............................................................................. 6
Fine-tuning repository definition ................................................................... 6
Defining query methods ...................................................................................... 7
Query lookup strategies .............................................................................. 7
Query creation ........................................................................................... 8
Property expressions .................................................................................. 9
Special parameter handling ........................................................................ 9
Creating repository instances ............................................................................ 10
XML configuration .................................................................................... 10
JavaConfig ............................................................................................... 11
Standalone usage .................................................................................... 11
3.3. Custom implementations for Spring Data repositories ........................................... 11
Adding custom behavior to single repositories .................................................... 11
Adding custom behavior to all repositories ......................................................... 13
3.4. Spring Data extensions ...................................................................................... 14
Web support .................................................................................................... 14
Basic web support .................................................................................... 15
Hypermedia support for Pageables ............................................................ 17
Repository populators ....................................................................................... 18
Legacy web support ......................................................................................... 19
Domain class web binding for Spring MVC ................................................ 19
Web pagination ........................................................................................ 21
II. Reference Documentation ..................................................................................................... 24
4. MongoDB support ........................................................................................................ 25
4.1. Getting Started .................................................................................................. 25
4.2. Examples Repository ......................................................................................... 28
4.3. Connecting to MongoDB with Spring .................................................................. 28
Registering a Mongo instance using Java based metadata ................................. 28
Registering a Mongo instance using XML based metadata .................................. 29
The MongoDbFactory interface ......................................................................... 30
Registering a MongoDbFactory instance using Java based metadata ................... 31
Registering a MongoDbFactory instance using XML based metadata ................... 32
4.4. General auditing configuration ............................................................................ 33
4.5. Introduction to MongoTemplate .......................................................................... 34
. Instantiating MongoTemplate ......................................................................... 35
WriteResultChecking Policy ....................................................................... 36
WriteConcern ........................................................................................... 36
WriteConcernResolver .............................................................................. 36
4.6. Saving, Updating, and Removing Documents ...................................................... 36
How the '_id' field is handled in the mapping layer .............................................. 39
Type mapping .................................................................................................. 40
Methods for saving and inserting documents ...................................................... 41
Which collection will my documents be saved into? .................................... 42
Inserting or saving individual objects ......................................................... 42
Inserting several objects in a batch ........................................................... 43
Updating documents in a collection ................................................................... 43
Methods for executing updates for documents ............................................ 43
Methods for the Update class ................................................................... 43
Upserting documents in a collection .................................................................. 44
Finding and Upserting documents in a collection ................................................ 44
Methods for removing documents ...................................................................... 45
4.7. Querying Documents ......................................................................................... 45
Querying documents in a collection ................................................................... 46
Methods for the Criteria class ................................................................... 46
Methods for the Query class ..................................................................... 47
Methods for querying for documents .................................................................. 48
GeoSpatial Queries .......................................................................................... 48
Geo near queries ..................................................................................... 50
4.8. Map-Reduce Operations .................................................................................... 50
Example Usage ................................................................................................ 51
4.9. Group Operations .............................................................................................. 52
Example Usage ................................................................................................ 53
4.10. Aggregation Framework Support ....................................................................... 54
Basic Concepts ................................................................................................ 55
Supported Aggregation Operations .................................................................... 55
Projection Expressions ..................................................................................... 56
Spring Expression Support in Projection Expressions ................................. 56
Aggregation Framework Examples .................................................................... 57
4.11. Overriding default mapping with custom converters ............................................ 62
Saving using a registered Spring Converter ....................................................... 62
Reading using a Spring Converter ..................................................................... 63
Registering Spring Converters with the MongoConverter ..................................... 63
Converter disambiguation ................................................................................. 64
4.12. Index and Collection management .................................................................... 64
Methods for creating an Index ........................................................................... 64
Accessing index information .............................................................................. 65
Methods for working with a Collection ............................................................... 65
4.13. Executing Commands ...................................................................................... 65
Methods for executing commands ..................................................................... 65
4.14. Lifecycle Events ............................................................................................... 66
4.15. Exception Translation ....................................................................................... 67
4.16. Execution callbacks ......................................................................................... 67
4.17. GridFS support ................................................................................................ 68
5. MongoDB repositories .................................................................................................. 71
5.1. Introduction ....................................................................................................... 71
5.2. Usage ............................................................................................................... 71
Preface
The Spring Data MongoDB project applies core Spring concepts to the development of solutions using
the MongoDB document style data store. We provide a "template" as a high-level abstraction for storing
and querying documents. You will notice similarities to the JDBC support in the Spring Framework.
This section provides some basic introduction to Spring and Document database. The rest of the
document refers only to Spring Data Document features and assumes the user is familiar with document
databases such as MongoDB and CouchDB as well as Spring concepts.
1 Knowing Spring
Spring Data uses Spring framework's core functionality, such as the IoC container, type conversion
system, expression language, JMX integration, and portable DAO exception hierarchy. While it is not
important to know the Spring APIs, understanding the concepts behind them is. At a minimum, the idea
behind IoC should be familiar for whatever IoC container you choose to use.
The core functionality of the MongoDB and CouchDB support can be used directly, with no need to
invoke the IoC services of the Spring Container. This is much like JdbcTemplate which can be used
'standalone' without any other services of the Spring container. To leverage all the features of Spring
Data document, such as the repository support, you will need to configure some parts of the library
using Spring.
To learn more about Spring, you can refer to the comprehensive (and sometimes disarming)
documentation that explains in detail the Spring Framework. There are a lot of articles, blog entries and
books on the matter - take a look at the Spring framework home page for more information.
The jumping off ground for learning about MongoDB is www.mongodb.org. Here is a list of other useful
resources.
• The manual introduces MongoDB and contains links to getting started guides, reference
documentation and tutorials.
• The online shell provides a convenient way to interact with a MongoDB instance in combination with
the online tutorial.
1. Requirements
Spring Data MongoDB 1.x binaries requires JDK level 6.0 and above, and Spring Framework 3.2.x and
above.
2.1 Support
There are a few support options available:
Community Forum
The Spring Data forum is a message board for all Spring Data (not just Document) users to share
information and help each other. Note that registration is needed only for posting.
Professional Support
Professional, from-the-source support, with guaranteed response time, is available from Pivotal
Sofware, Inc., the company behind Spring Data and Spring.
You can help make Spring Data best serve the needs of the Spring community by interacting with
developers through the Spring Community forums. To follow developer activity look for the mailing list
information on the Spring Data Mongo homepage.
If you encounter a bug or want to suggest an improvement, please create a ticket on the Spring Data
issue tracker.
To stay up to date with the latest news and announcements in the Spring eco system, subscribe to the
Spring Community Portal.
Lastly, you can follow the SpringSource Data blog or the project team on Twitter (SpringData)
Important
Spring Data repository documentation and your module
This chapter explains the core concepts and interfaces of Spring Data repositories. The
information in this chapter is pulled from the Spring Data Commons module. It uses the
configuration and code samples for the Java Persistence API (JPA) module. Adapt the XML
namespace declaration and the types to be extended to the equivalents of the particular module
that you are using. Appendix A, Namespace reference covers XML configuration which is
supported across all Spring Data modules supporting the repository API, Appendix B, Repository
query keywords covers the query method keywords supported by the repository abstraction in
general. For detailed information on the specific features of your module, consult the chapter on
that module of this document.
Long count();
❹
void delete(T entity);
❺
boolean exists(ID primaryKey);
❻
// … more functionality omitted.
}
Note
We also provide persistence technology-specific abstractions like e.g. JpaRepository or
MongoRepository. Those interfaces extend CrudRepository and expose the capabilities of
the underlying persistence technology in addition to the rather generic persistence technology-
agnostic interfaces like e.g. CrudRepository.
Accessing the second page of User by a page size of 20 you could simply do something like this:
1. Declare an interface extending Repository or one of its subinterfaces and type it to the domain
class and ID type that it will handle.
3. Set up Spring to create proxy instances for those interfaces. Either via JavaConfig:
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories
class Config {}
<jpa:repositories base-package="com.acme.repositories"/>
</beans>
The JPA namespace is used in this example. If you are using the repository abstraction for any other
store, you need to change this to the appropriate namespace declaration of your store module which
should be exchanging jpa in favor of, for example, mongodb. Also, note that the JavaConfig variant
doesn't configure a package explictly as the package of the annotated class is used by default. To
customize the package to scan
@Autowired
private PersonRepository repository;
As a first step you define a domain class-specific repository interface. The interface must extend
Repository and be typed to the domain class and an ID type. If you want to expose CRUD methods
for that domain type, extend CrudRepository instead of Repository.
Note
This allows you to define your own abstractions on top of the provided Spring Data Repositories
functionality.
@NoRepositoryBean
interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
T findOne(ID id);
T save(T entity);
}
In this first step you defined a common base interface for all your domain repositories and exposed
findOne(…) as well as save(…).These methods will be routed into the base repository implementation
of the store of your choice provided by Spring Data ,e.g. in the case if JPA SimpleJpaRepository,
because they are matching the method signatures in CrudRepository. So the UserRepository will
now be able to save users, and find single ones by id, as well as triggering a query to find Users by
their email address.
Note
Note, that the intermediate repository interface is annotated with @NoRepositoryBean. Make
sure you add that annotation to all repository interfaces that Spring Data should not create
instances for at runtime.
The repository proxy has two ways to derive a store-specific query from the method name. It can derive
the query from the method name directly, or by using an manually defined query. Available options
depend on the actual store. However, there's got to be an strategy that decides what actual query is
created. Let's have a look at the available options.
The following strategies are available for the repository infrastructure to resolve the query. You
can configure the strategy at the namespace through the query-lookup-strategy attribute
in case of XML configuration or via the queryLookupStrategy attribute of the Enable
${store}Repositories annotation in case of Java config. Some strategies may not be supported
for particular datastores.
CREATE
CREATE attempts to construct a store-specific query from the query method name. The general approach
is to remove a given set of well-known prefixes from the method name and parse the rest of the method.
Read more about query construction in the section called “Query creation”.
USE_DECLARED_QUERY
USE_DECLARED_QUERY tries to find a declared query and will throw an exception in case it can't find
one. The query can be defined by an annotation somewhere or declared by other means. Consult the
documentation of the specific store to find available options for that store. If the repository infrastructure
does not find a declared query for the method at bootstrap time, it fails.
CREATE_IF_NOT_FOUND (default)
Query creation
The query builder mechanism built into Spring Data repository infrastructure is useful for building
constraining queries over entities of the repository. The mechanism strips the prefixes find…By,
read…By, query…By, count…By, and get…By from the method and starts parsing the rest of it. The
introducing clause can contain further expressions such as a Distinct to set a distinct flag on the
query to be created. However, the first By acts as delimiter to indicate the start of the actual criteria. At a
very basic level you can define conditions on entity properties and concatenate them with And and Or.
The actual result of parsing the method depends on the persistence store for which you create the query.
However, there are some general things to notice.
• The expressions are usually property traversals combined with operators that can be concatenated.
You can combine property expressions with AND and OR. You also get support for operators such as
Between, LessThan, GreaterThan, Like for the property expressions. The supported operators
can vary by datastore, so consult the appropriate part of your reference documentation.
• The method parser supports setting an IgnoreCase flag for individual properties, for
example,findByLastnameIgnoreCase(…)) or for all properties of a type that support ignoring case
(usually Strings, for example, findByLastnameAndFirstnameAllIgnoreCase(…)). Whether
ignoring cases is supported may vary by store, so consult the relevant sections in the reference
documentation for the store-specific query method.
• You can apply static ordering by appending an OrderBy clause to the query method that references
a property and by providing a sorting direction (Asc or Desc). To create a query method that supports
dynamic sorting, see the section called “Special parameter handling”.
Property expressions
Property expressions can refer only to a direct property of the managed entity, as shown in the preceding
example. At query creation time you already make sure that the parsed property is a property of the
managed domain class. However, you can also define constraints by traversing nested properties.
Assume Persons have Addresses with ZipCodes. In that case a method name of
creates the property traversal x.address.zipCode. The resolution algorithm starts with interpreting
the entire part (AddressZipCode) as the property and checks the domain class for a property with
that name (uncapitalized). If the algorithm succeeds it uses that property. If not, the algorithm splits
up the source at the camel case parts from the right side into a head and a tail and tries to find the
corresponding property, in our example, AddressZip and Code. If the algorithm finds a property with
that head it takes the tail and continue building the tree down from there, splitting the tail up in the way
just described. If the first split does not match, the algorithm move the split point to the left (Address,
ZipCode) and continues.
Although this should work for most cases, it is possible for the algorithm to select the wrong property.
Suppose the Person class has an addressZip property as well. The algorithm would match in the first
split round already and essentially choose the wrong property and finally fail (as the type of addressZip
probably has no code property). To resolve this ambiguity you can use _ inside your method name to
manually define traversal points. So our method name would end up like so:
To handle parameters in your query you simply define method parameters as already seen in the
examples above. Besides that the infrastructure will recognize certain specific types like Pageable and
Sort to apply pagination and sorting to your queries dynamically.
Note
To find out how many pages you get for a query entirely you have to trigger an additional count
query. By default this query will be derived from the query you actually trigger.
In this section you create instances and bean definitions for the repository interfaces defined. One way
to do so is using the Spring namespace that is shipped with each Spring Data module that supports the
repository mechanism although we generally recommend to use the Java-Config style configuration.
XML configuration
Each Spring Data module includes a repositories element that allows you to simply define a base
package that Spring scans for you.
</beans:beans>
In the preceding example, Spring is instructed to scan com.acme.repositories and all its subpackages
for interfaces extending Repository or one of its subinterfaces. For each interface found, the
infrastructure registers the persistence technology-specific FactoryBean to create the appropriate
proxies that handle invocations of the query methods. Each bean is registered under a bean name that
is derived from the interface name, so an interface of UserRepository would be registered under
userRepository. The base-package attribute allows wildcards, so that you can define a pattern of
scanned packages.
Using filters
By default the infrastructure picks up every interface extending the persistence technology-specific
Repository subinterface located under the configured base package and creates a bean instance
for it. However, you might want more fine-grained control over which interfaces bean instances get
created for. To do this you use <include-filter /> and <exclude-filter /> elements
inside <repositories />. The semantics are exactly equivalent to the elements in Spring's context
namespace. For details, see Spring reference documentation on these elements.
For example, to exclude certain interfaces from instantiation as repository, you could use the following
configuration:
<repositories base-package="com.acme.repositories">
<context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>
This example excludes all interfaces ending in SomeRepository from being instantiated.
Example 3.6 Using exclude-filter element
2
JavaConfig in the Spring reference documentation - http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/
html/beans.html#beans-java
JavaConfig
A sample configuration to enable Spring Data repositories looks something like this.
@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {
@Bean
public EntityManagerFactory entityManagerFactory() {
// …
}
}
Note
The sample uses the JPA-specific annotation, which you would change according to the store
module you actually use. The same applies to the definition of the EntityManagerFactory
bean. Consult the sections covering the store-specific configuration.
Standalone usage
You can also use the repository infrastructure outside of a Spring container, e.g. in CDI environments.
You still need some Spring libraries in your classpath, but generally you can set up repositories
programmatically as well. The Spring Data modules that provide repository support ship a persistence
technology-specific RepositoryFactory that you can use as follows.
To enrich a repository with custom functionality you first define an interface and an implementation for
the custom functionality. Use the repository interface you provided to extend the custom interface.
interface UserRepositoryCustom {
Note
The implementation itself does not depend on Spring Data and can be a regular Spring bean.
So you can use standard dependency injection behavior to inject references to other beans like
a JdbTemplate, take part in aspects, and so on.
Let your standard repository interface extend the custom one. Doing so combines the CRUD and custom
functionality and makes it available to clients.
Example 3.11 Changes to the your basic repository interface
Configuration
If you use namespace configuration, the repository infrastructure tries to autodetect custom
implementations by scanning for classes below the package we found a repository in. These classes
need to follow the naming convention of appending the namespace element's attribute repository-
impl-postfix to the found repository interface name. This postfix defaults to Impl.
Manual wiring
The preceding approach works well if your custom implementation uses annotation-based configuration
and autowiring only, as it will be treated as any other Spring bean. If your custom implementation bean
needs special wiring, you simply declare the bean and name it after the conventions just described.
The infrastructure will then refer to the manually defined bean definition by name instead of creating
one itself.
The preceding approach is not feasible when you want to add a single method to all your repository
interfaces.
1. To add custom behavior to all repositories, you first add an intermediate interface to declare the
shared behavior.
Now your individual repository interfaces will extend this intermediate interface instead of the
Repository interface to include the functionality declared.
2. Next, create an implementation of the intermediate interface that extends the persistence technology-
specific repository base class. This class will then act as a custom base class for the repository
proxies.
The default behavior of the Spring <repositories /> namespace is to provide an implementation
for all interfaces that fall under the base-package. This means that if left in its current state, an
implementation instance of MyRepository will be created by Spring. This is of course not desired
as it is just supposed to act as an intermediary between Repository and the actual repository
interfaces you want to define for each entity. To exclude an interface that extends Repository from
being instantiated as a repository instance, you can either annotate it with @NoRepositoryBean or
move it outside of the configured base-package.
3. Then create a custom repository factory to replace the default RepositoryFactoryBean that will
in turn produce a custom RepositoryFactory. The new repository factory will then provide your
MyRepositoryImpl as the implementation of any interfaces that extend the Repository interface,
replacing the SimpleJpaRepository implementation you just extended.
this.entityManager = entityManager;
}
4. Finally, either declare beans of the custom factory directly or use the factory-class attribute of
the Spring namespace to tell the repository infrastructure to use your custom factory implementation.
<repositories base-package="com.acme.repository"
factory-class="com.acme.MyRepositoryFactoryBean" />
Web support
Note
This section contains the documentation for the Spring Data web support as it is implemented
as of Spring Data Commons in the 1.6 range. As it the newly introduced support changes quite
a lot of things we kept the documentation of the former behavior in the section called “Legacy
web support”.
Also note that the JavaConfig support introduced in Spring Data Commons 1.6 requires Spring
3.2 due to some issues with JavaConfig and overridden methods in Spring 3.1.
Spring Data modules ships with a variety of web support if the module supports the repository
programming model. The web related stuff requires Spring MVC JARs on the classpath, some of them
even provide integration with Spring HATEOAS.
3
In general, the integration support is enabled by using the @EnableSpringDataWebSupport
annotation in your JavaConfig configuration class.
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
class WebConfiguration { }
<!-- If you're using Spring HATEOAS as well register this one *instead* of the former -->
<bean class="org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration" /
>
The configuration setup shown above will register a few basic components:
DomainClassConverter
The DomainClassConverter allows you to use domain types in your Spring MVC controller method
signatures directly, so that you don't have to manually lookup the instances via the repository:
3
Spring HATEOAS - https://github.com/SpringSource/spring-hateoas
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping("/{id}")
public String showUserForm(@PathVariable("id") User user, Model model) {
model.addAttribute("user", user);
return "userForm";
}
}
Example 3.20 A Spring MVC controller using domain types in method signatures
As you can see the method receives a User instance directly and no further lookup is necessary. The
instance can be resolved by letting Spring MVC convert the path variable into the id type of the domain
class first and eventually access the instance through calling findOne(…) on the repository instance
registered for the domain type.
Note
Currently the repository has to implement CrudRepository to be eligible to be discovered for
conversion.
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping
public String showUsers(Model model, Pageable pageable) {
model.addAttribute("users", repository.findAll(pageable));
return "users";
}
}
This method signature will cause Spring MVC try to derive a Pageable instance from the request
parameters using the following default configuration:
In case you need multiple Pageables or Sorts to be resolved from the request (for multiple tables, for
example) you can use Spring's @Qualifier annotation to distinguish one from another. The request
parameters then have to be prefixed with ${qualifier}_. So for a method signature like this:
The default Pageable handed into the method is equivalent to a new PageRequest(0, 20) but can
be customized using the @PageableDefaults annotation on the Pageable parameter.
Spring HATEOAS ships with a representation model class PagedResources that allows enrichting the
content of a Page instance with the necessary Page metadata as well as links to let the clients easily
navigate the pages. The conversion of a Page to a PagedResources is done by an implementation of
the Spring HATEOAS ResourceAssembler interface, the PagedResourcesAssembler.
@Controller
class PersonController {
• The content of the Page will become the content of the PagedResources instance.
• The PagedResources will get a PageMetadata instance attached populated with information form
the Page and the underlying PageRequest.
• The PagedResources gets prev and next links attached depending on the page's state. The links
will point to the URI the method invoked is mapped to. The pagination parameters added to the method
will match the setup of the PageableHandlerMethodArgumentResolver to make sure the links
can be resolved later on.
Assume we have 30 Person instances in the database. You can now trigger a request GET http://
localhost:8080/persons and you'll see something similar to this:
You see that the assembler produced the correct URI and also picks up the default configuration
present to resolve the parameters into a Pageable for an upcoming request. This means, if
you change that configuration, the links will automatically adhere to the change. By default the
assembler points to the controller method it was invoked in but that can be customized by
handing in a custom Link to be used as base to build the pagination links to overloads of the
PagedResourcesAssembler.toResource(…) method.
Repository populators
If you work with the Spring JDBC module, you probably are familiar with the support to populate a
DataSource using SQL scripts. A similar abstraction is available on the repositories level, although
it does not use SQL as the data definition language because it must be store-independent. Thus the
populators support XML (through Spring's OXM abstraction) and JSON (through Jackson) to define data
with which to populate the repositories.
[ { "_class" : "com.acme.Person",
"firstname" : "Dave",
"lastname" : "Matthews" },
{ "_class" : "com.acme.Person",
"firstname" : "Carter",
"lastname" : "Beauford" } ]
You can easily populate your repositories by using the populator elements of the repository namespace
provided in Spring Data Commons. To populate the preceding data to your PersonRepository , do
the following:
</beans>
This declaration causes the data.json file to be read and deserialized via a Jackson ObjectMapper.
The type to which the JSON object will be unmarshalled to will be determined by inspecting the _class
attribute of the JSON document. The infrastructure will eventually select the appropriate repository to
handle the object just deserialized.
To rather use XML to define the data the repositories shall be populated with, you can use the
unmarshaller-populator element. You configure it to use one of the XML marshaller options Spring
OXM provides you with. See the Spring reference documentation for details.
</beans>
Given you are developing a Spring MVC web application you typically have to resolve domain class
ids from URLs. By default your task is to transform that request parameter or URL part into the domain
class to hand it to layers below then or execute business logic on the entities directly. This would look
something like this:
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
public UserController(UserRepository userRepository) {
Assert.notNull(repository, "Repository must not be null!");
userRepository = userRepository;
}
@RequestMapping("/{id}")
public String showUserForm(@PathVariable("id") Long id, Model model) {
model.addAttribute("user", user);
return "user";
}
}
First you declare a repository dependency for each controller to look up the entity managed by
the controller or repository respectively. Looking up the entity is boilerplate as well, as it's always
a findOne(…) call. Fortunately Spring provides means to register custom components that allow
conversion between a String value to an arbitrary type.
PropertyEditors
For Spring versions before 3.0 simple Java PropertyEditors had to be used. To integrate with
that, Spring Data offers a DomainClassPropertyEditorRegistrar, which looks up all Spring Data
repositories registered in the ApplicationContext and registers a custom PropertyEditor for
the managed domain class.
<bean class="….web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="….web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrars">
<bean class="org.springframework.data.repository.support.DomainClassPropertyEditorRegistrar"
/>
</property>
</bean>
</property>
</bean>
If you have configured Spring MVC as in the preceding example, you can configure your controller as
follows, which reduces a lot of the clutter and boilerplate.
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping("/{id}")
public String showUserForm(@PathVariable("id") User user, Model model) {
model.addAttribute("user", user);
return "userForm";
}
}
ConversionService
In Spring 3.0 and later the PropertyEditor support is superseded by a new conversion infrastructure
that eliminates the drawbacks of PropertyEditors and uses a stateless X to Y conversion
approach. Spring Data now ships with a DomainClassConverter that mimics the behavior of
DomainClassPropertyEditorRegistrar. To configure, simply declare a bean instance and pipe
the ConversionService being used into its constructor:
<bean class="org.springframework.data.repository.support.DomainClassConverter">
<constructor-arg ref="conversionService" />
</bean>
If you are using JavaConfig, you can simply extend Spring MVC's WebMvcConfigurationSupport
and hand the FormatingConversionService that the configuration superclass provides into the
DomainClassConverter instance you create.
@Bean
public DomainClassConverter<?> domainClassConverter() {
return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
}
}
Web pagination
When working with pagination in the web layer you usually have to write a lot of boilerplate code yourself
to extract the necessary metadata from the request. The less desirable approach shown in the example
below requires the method to contain an HttpServletRequest parameter that has to be parsed
manually. This example also omits appropriate failure handling, which would make the code even more
verbose.
@Controller
@RequestMapping("/users")
public class UserController {
// DI code omitted
@RequestMapping
public String showUsers(Model model, HttpServletRequest request) {
model.addAttribute("users", userService.getUsers(pageable));
return "users";
}
}
The bottom line is that the controller should not have to handle the functionality of extracting pagination
information from the request. So Spring Data ships with a PageableHandlerArgumentResolver
that will do the work for you. The Spring MVC JavaConfig support exposes a
WebMvcConfigurationSupport helper class to customize the configuration as follows:
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new PageableHandlerArgumentResolver());
}
}
If you're stuck with XML configuration you can register the resolver as follows:
<bean class="….web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="customArgumentResolvers">
<list>
<bean class="org.springframework.data.web.PageableHandlerArgumentResolver" />
</list>
</property>
</bean>
When using Spring 3.0.x versions use the PageableArgumentResolver instead. Once you've
configured the resolver with Spring MVC it allows you to simplify controllers down to something like this:
@Controller
@RequestMapping("/users")
public class UserController {
@RequestMapping
public String showUsers(Model model, Pageable pageable) {
model.addAttribute("users", userRepository.findAll(pageable));
return "users";
}
}
In case you need multiple Pageables to be resolved from the request (for multiple tables, for example)
you can use Spring's @Qualifier annotation to distinguish one from another. The request parameters
then have to be prefixed with ${qualifier}_. So for a method signature like this:
you have to populate foo_page and bar_page and the related subproperties.
The PageableArgumentResolver will use a PageRequest with the first page and a page size of 10
by default. It will use that value if it cannot resolve a PageRequest from the request (because of missing
parameters, for example). You can configure a global default on the bean declaration directly. If you
might need controller method specific defaults for the Pageable, annotate the method parameter with
@PageableDefaults and specify page (through pageNumber), page size (through value), sort (list
of properties to sort by), and sortDir (the direction to sort by) as annotation attributes:
Document Structure
This part of the reference documentation explains the core functionality offered by Spring Data
Document.
4. MongoDB support
The MongoDB support contains a wide range of features which are summarized below.
• Spring configuration support using Java based @Configuration classes or an XML namespace for a
Mongo driver instance and replica sets
• MongoTemplate helper class that increases productivity performing common Mongo operations.
Includes integrated object mapping between documents and POJOs.
• Annotation based mapping metadata but extensible to support other metadata formats
• Automatic implementation of Repository interfaces including support for custom finder methods.
• Cross-store persistance - support for JPA Entities with fields transparently persisted/retrieved using
MongoDB
• GeoSpatial integration
For most tasks you will find yourself using MongoTemplate or the Repository support that both leverage
the rich mapping functionality. MongoTemplate is the place to look for accessing functionality such as
incrementing counters or ad-hoc CRUD operations. MongoTemplate also provides callback methods so
that it is easy for you to get a hold of the low level API artifacts such as org.mongo.DB to communicate
directly with MongoDB. The goal with naming conventions on various API artifacts is to copy those in
the base MongoDB Java driver so you can easily map your existing knowledge onto the Spring APIs.
First you need to set up a running Mongodb server. Refer to the Mongodb Quick Start guide for an
explanation on how to startup a MongoDB instance. Once installed starting MongoDB is typically a
matter of executing the following command: MONGO_HOME/bin/mongod
To create a Spring project in STS go to File -> New -> Spring Template Project -> Simple Spring
Utility Project -> press Yes when prompted. Then enter a project and a package name such as
org.spring.mongodb.example.
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
</dependencies>
<spring.framework.version>3.2.8.RELEASE</spring.framework.version>
You will also need to add the location of the Spring Milestone repository for maven to your pom.xml
which is at the same level of your <dependencies/> element
<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>http://repo.spring.io/libs-milestone</url>
</repository>
</repositories>
You may also want to set the logging level to DEBUG to see some additional information, edit the
log4j.properties file to have
log4j.category.org.springframework.data.document.mongodb=DEBUG
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n
package org.spring.mongodb.example;
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
package org.spring.mongodb.example;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.Mongo;
mongoOps.dropCollection("person");
}
}
Even in this simple example, there are few things to take notice of
• You can instantiate the central helper class of Spring Mongo, MongoTemplate, using the standard
com.mongodb.Mongo object and the name of the database to use.
• The mapper works against standard POJO objects without the need for any additional metadata
(though you can optionally provide that information. See here.).
• Conventions are used for handling the id field, converting it to be a ObjectId when stored in the
database.
• Mapping conventions can use field access. Notice the Person class has only getters.
• If the constructor argument names match the field names of the stored document, they will be used
to instantiate the object
Note
For those not familiar with how to configure the Spring container using Java based bean metadata
instead of XML based metadata see the high level introduction in the reference docs here as
well as the detailed documentation here.
@Configuration
public class AppConfig {
/*
* Use the standard Mongo driver API to create a com.mongodb.Mongo instance.
*/
public @Bean Mongo mongo() throws UnknownHostException {
return new Mongo("localhost");
}
}
Example 4.1 Registering a com.mongodb.Mongo object using Java based bean metadata
This approach allows you to use the standard com.mongodb.Mongo API that you may already be used
to using but also pollutes the code with the UnknownHostException checked exception. The use of the
checked exception is not desirable as Java based bean metadata uses methods as a means to set
object dependencies, making the calling code cluttered.
An example of a Java based bean metadata that supports exception translation on @Repository
annotated classes is shown below:
@Configuration
public class AppConfig {
/*
* Factory bean that creates the com.mongodb.Mongo instance
*/
public @Bean MongoFactoryBean mongo() {
MongoFactoryBean mongo = new MongoFactoryBean();
mongo.setHost("localhost");
return mongo;
}
}
While you can use Spring's traditional <beans/> XML namespace to register an instance of
com.mongodb.Mongo with the container, the XML can be quite verbose as it is general purpose. XML
namespaces are a better alternative to configuring commonly used objects such as the Mongo instance.
The mongo namespace alows you to create a Mongo instance server location, replica-sets, and options.
To use the Mongo namespace elements you will need to reference the Mongo schema:
</beans>
A more advanced configuration with MongoOptions is shown below (note these are not recommended
values)
<beans>
</beans>
Example 4.5 XML schema to configure com.mongodb.Mongo object with Replica Sets
While com.mongodb.Mongo is the entry point to the MongoDB driver API, connecting to a specific
MongoDB database instance requires additional information such as the database name and
an optional username and password. With that information you can obtain a com.mongodb.DB
object and access all the functionality of a specific MongoDB database instance. Spring provides
the org.springframework.data.mongodb.core.MongoDbFactory interface shown below to
bootstrap connectivity to the database.
The following sections show how you can use the container with either Java or the XML based metadata
to configure an instance of the MongoDbFactory interface. In turn, you can use the MongoDbFactory
instance to configure MongoTemplate.
Instead of using the IoC container to create an instance of MongoTemplate, you can just use them in
standard Java code as shown below.
mongoOps.dropCollection("person");
}
}
The code in bold highlights the use of SimpleMongoDbFactory and is the only difference between the
listing shown in the getting started section.
To register a MongoDbFactory instance with the container, you write code much like what was
highlighted in the previous code listing. A simple example is shown below
@Configuration
public class MongoConfiguration {
@Configuration
public class MongoConfiguration {
<mongo:db-factory dbname="database">
In the above example a com.mongodb.Mongo instance is created using the default host and
port number. The SimpleMongoDbFactory registered with the container is identified by the id
'mongoDbFactory' unless a value for the id attribute is specified.
You can also provide the host and port for the underlying com.mongodb.Mongo instance as shown
below, in addition to username and password for the database.
<mongo:db-factory id="anotherMongoDbFactory"
host="localhost"
port="27017"
dbname="database"
username="joe"
password="secret"/>
If you need to configure additional options on the com.mongodb.Mongo instance that is used to create
a SimpleMongoDbFactory you can refer to an existing bean using the mongo-ref attribute as shown
below. To show another common usage pattern, this listing show the use of a property placeholder to
parameterise the configuration and creating MongoTemplate.
<context:property-placeholder location="classpath:/com/myapp/mongodb/config/
mongo.properties"/>
Since Spring Data MongoDB 1.4 auditing can be enabled by annotating a configuration class with the
@EnableMongoAuditing annotation.
@Configuration
@EnableMongoAuditing
class Config {
@Bean
public AuditorAware<AuditableUser> myAuditorProvider() {
return new AuditorAwareImpl();
}
}
If you expose a bean of type AuditorAware to the ApplicationContext, the auditing infrastructure
will pick it up automatically and use it to determine the current user to be set on domain types. If you
have multiple implementations registered in the ApplicationContext, you can select the one to be
used by explicitly setting the auditorAwareRef attribute of @EnableJpaAuditing.
Note
Once configured, MongoTemplate is thread-safe and can be reused across multiple instances.
The mapping between MongoDB documents and domain classes is done by delegating to
an implementation of the interface MongoConverter. Spring provides two implementations,
SimpleMappingConverter and MongoMappingConverter, but you can also write your own
converter. Please refer to the section on MongoCoverters for more detailed information.
Note
The preferred way to reference the operations on MongoTemplate instance is via its interface
MongoOperations.
Note
In the M2 release SimpleMappingConverter, was the default and this class is now deprecated
as its functionality has been subsumed by the MongoMappingConverter.
Another central feature of MongoTemplate is exception translation of exceptions thrown in the MongoDB
Java driver into Spring's portable Data Access Exception hierarchy. Refer to the section on exception
translation for more information.
While there are many convenience methods on MongoTemplate to help you easily perform common
tasks if you should need to access the MongoDB driver API directly to access functionality not
explicitly exposed by the MongoTemplate you can use one of several Execute callback methods
to access underlying driver APIs. The execute callbacks will give you a reference to either a
Now let's look at a examples of how to work with the MongoTemplate in the context of the Spring
container.
Instantiating MongoTemplate
You can use Java to create and register an instance of MongoTemplate as shown below.
@Configuration
public class AppConfig {
Example 4.8 Registering a com.mongodb.Mongo object and enabling Spring's exception translation
support
You can also configure a MongoTemplate using Spring's XML <beans/> schema.
Other optional properties that you might like to set when creating a MongoTemplate are the default
WriteResultCheckingPolicy, WriteConcern, and ReadPreference.
Note
The preferred way to reference the operations on MongoTemplate instance is via its interface
MongoOperations.
WriteResultChecking Policy
WriteConcern
You can set the com.mongodb.WriteConcern property that the MongoTemplate will use for write
operations if it has not yet been specified via the driver at a higher level such as com.mongodb.Mongo.
If MongoTemplate's WriteConcern property is not set it will default to the one set in the MongoDB
driver's DB or Collection setting.
WriteConcernResolver
For more advanced cases where you want to set different WriteConcern values on a per-
operation basis (for remove, update, insert and save operations), a strategy interface called
WriteConcernResolver can be configured on MongoTemplate. Since MongoTemplate is used to
persist POJOs, the WriteConcernResolver lets you create a policy that can map a specific POJO
class to a WriteConcern value. The WriteConcernResolver interface is shown below.
The passed in argument, MongoAction, is what you use to determine the WriteConcern value
to be used or to use the value of the Template itself as a default. MongoAction contains the
collection name being written to, the java.lang.Class of the POJO, the converted DBObject, as
well as the operation as an enumeration (MongoActionOperation: REMOVE, UPDATE, INSERT,
INSERT_LIST, SAVE) and a few other pieces of contextual information. For example,
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
You can save, update and delete the object as shown below.
Note
MongoOperations is the interface that MongoTemplate implements.
package org.spring.example;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.Mongo;
// Find
p = mongoOps.findById(p.getId(), Person.class);
log.info("Found: " + p);
// Update
mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class);
p = mongoOps.findOne(query(where("name").is("Joe")), Person.class);
log.info("Updated: " + p);
// Delete
mongoOps.remove(p);
mongoOps.dropCollection(Person.class);
}
}
This would produce the following log output (including debug messages from MongoTemplate itself)
There was implicit conversion using the MongoConverter between a String and ObjectId as stored
in the database and recognizing a convention of the property "Id" name.
Note
This example is meant to show the use of save, update and remove operations on
MongoTemplate and not to show complex mapping functionality
The query syntax used in the example is explained in more detail in the section Querying Documents.
The following outlines what property will be mapped to the '_id' document field:
• A property or field without an annotation but named id will be mapped to the '_id' field.
The following outlines what type conversion, if any, will be done on the property mapped to the _id
document field when using the MappingMongoConverter, the default for MongoTemplate.
• An id property or field declared as a String in the Java class will be converted to and stored as an
ObjectId if possible using a Spring Converter<String, ObjectId>. Valid conversion rules are
delegated to the MongoDB Java driver. If it cannot be converted to an ObjectId, then the value will
be stored as a string in the database.
• An id property or field declared as BigInteger in the Java class will be converted to and stored as
an ObjectId using a Spring Converter<BigInteger, ObjectId>.
If no field or property specified above is present in the Java class then an implicit '_id' file will be
generated by the driver but not mapped to a property or field of the Java class.
When querying and updating MongoTemplate will use the converter to handle conversions of the
Query and Update objects that correspond to the above rules for saving documents so field names
and types used in your queries will be able to match what is in your domain classes.
Type mapping
As MongoDB collections can contain documents that represent instances of a variety of types. A great
example here is if you store a hierarchy of classes or simply have a class with a property of type Object.
In the latter case the values held inside that property have to be read in correctly when retrieving the
object. Thus we need a mechanism to store type information alongside the actual document.
mongoTemplate.save(sample);
{ "_class" : "com.acme.Sample",
"value" : { "_class" : "com.acme.Person" }
}
As you can see we store the type information for the actual root class persistent as well
as for the nested type as it is complex and a subtype of Contact. So if you're now using
mongoTemplate.findAll(Object.class, "sample") we are able to find out that the document
stored shall be a Sample instance. We are also able to find out that the value property shall be a Person
actually.
In case you want to avoid writing the entire Java class name as type information but rather like to use
some key you can use the @TypeAlias annotation at the entity class being persisted. If you need to
customize the mapping even more have a look at the TypeInformationMapper interface. An instance
of that interface can be configured at the DefaultMongoTypeMapper which can be configured in turn
on MappingMongoConverter.
@TypeAlias("pers")
class Person {
Note that the resulting document will contain "pers" as the value in the _class Field.
Example 4.10 Defining a TypeAlias for an Entity
@Configuration
class SampleMongoConfiguration extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "database";
}
@Override
public Mongo mongo() throws Exception {
return new Mongo();
}
@Bean
@Override
public MappingMongoConverter mappingMongoConverter() throws Exception {
MappingMongoConverter mmc = super.mappingMongoConverter();
mmc.setTypeMapper(customTypeMapper());
return mmc;
}
@Bean
public MongoTypeMapper customTypeMapper() {
return new CustomMongoTypeMapper();
}
}
Note that we are extending the AbstractMongoConfiguration class and override the bean
definition of the MappingMongoConverter where we configure our custom MongoTypeMapper.
Example 4.11 Configuring a custom MongoTypeMapper via Spring Java Config
<mongo:mapping-converter type-mapper-ref="customMongoTypeMapper"/>
There are several convenient methods on MongoTemplate for saving and inserting your
objects. To have more fine grained control over the conversion process you can register Spring
converters with the MappingMongoConverter, for example Converter<Person, DBObject> and
Converter<DBObject, Person>.
Note
The difference between insert and save operations is that a save operation will perform an insert
if the object is not already present.
The simple case of using the save operation is to save a POJO. In this case the collection name will be
determined by name (not fully qualfied) of the class. You may also call the save operation with a specific
collection name. The collection to store the object can be overriden using mapping metadata.
When inserting or saving, if the Id property is not set, the assumption is that its value will be auto-
generated by the database. As such, for auto-generation of an ObjectId to succeed the type of the Id
property/field in your class must be either a String, ObjectId, or BigInteger.
Here is a basic example of using the save operation and retrieving its contents.
• void save (Object objectToSave) Save the object to the default collection.
• void save (Object objectToSave, String collectionName) Save the object to the
specified collection.
• void insert (Object objectToSave) Insert the object to the default collection.
• void insert (Object objectToSave, String collectionName) Insert the object to the
specified collection.
There are two ways to manage the collection name that is used for operating on the documents.
The default collection name that is used is the class name changed to start with a lower-case letter.
So a com.test.Person class would be stored in the "person" collection. You can customize this
by providing a different collection name using the @Document annotation. You can also override
the collection name by providing your own collection name as the last parameter for the selected
MongoTemplate method calls.
The MongoDB driver supports inserting a collection of documents in one operation. The methods in the
MongoOperations interface that support this functionality are listed below
• insert Insert an object. If there is an existing document with the same id then an error is generated.
• insertAll Takes a Collection of objects as the first parameter. This method inspects each object
and inserts it to the appropriate collection based on the rules specified above.
• save Save the object overwriting any object that might exist with the same id.
The MongoDB driver supports inserting a collection of documents in one operation. The methods in the
MongoOperations interface that support this functionality are listed below
• insert methods that take a Collection as the first argument.This inserts a list of
objects in a single batch write to the database.
For updates we can elect to update the first document found using MongoOperation's method
updateFirst or we can update all documents that were found to match the query using the method
updateMulti. Here is an example of an update of all SAVINGS accounts where we are adding a one
time $50.00 bonus to the balance using the $inc operator.
...
WriteResult wr = mongoTemplate.updateMulti(new
Query(where("accounts.accountType").is(Account.Type.SAVINGS)),
new Update().inc("accounts.
$.balance", 50.00),
Account.class);
In addition to the Query discussed above we provide the update definition using an Update object. The
Update class has methods that match the update modifiers available for MongoDB.
As you can see most methods return the Update object to provide a fluent style for the API.
• updateFirst Updates the first document that matches the query document criteria with the provided
updated document.
• updateMulti Updates all objects that match the query document criteria with the provided updated
document.
The Update class can be used with a little 'syntax sugar' as its methods are meant to be chained together
and you can kick-start the creation of a new Update instance via the static method public static
Update update(String key, Object value) and using static imports.
• Update addToSet (String key, Object value) Update using the $addToSet update
modifier
• Update inc (String key, Number inc) Update using the $inc update modifier
• Update pop (String key, Update.Position pos) Update using the $pop update modifier
• Update pull (String key, Object value) Update using the $pull update modifier
• Update pullAll (String key, Object[] values) Update using the $pullAll update modifier
• Update push (String key, Object value) Update using the $push update modifier
• Update pushAll (String key, Object[] values) Update using the $pushAll update
modifier
• Update rename (String oldName, String newName) Update using the $rename update
modifier
• Update set (String key, Object value) Update using the $set update modifier
• Update unset (String key) Update using the $unset update modifier
Related to performing an updateFirst operations, you can also perform an upsert operation which
will perform an insert if no document is found that matches the query. The document that is inserted is
a combination of the query document and the update document. Here is an example
template.upsert(query(where("ssn").is(1111).and("firstName").is("Joe").and("Fraizer").is("Update")),
update("address", addr), Person.class);
The findAndModify(…) method on DBCollection can update a document and return either the old or
newly updated document in a single operation. MongoTemplate provides a findAndModify method that
takes Query and Update classes and converts from DBObject to your POJOs. Here are the methods
As an example usage, we will insert of few Person objects into the container and perform a simple
findAndUpdate operation
assertThat(p.getFirstName(), is("Harry"));
assertThat(p.getAge(), is(23));
p = mongoTemplate.findOne(query, Person.class);
assertThat(p.getAge(), is(24));
The FindAndModifyOptions lets you set the options of returnNew, upsert, and remove. An example
extending off the previous code snippit is shown below
You can use several overloaded methods to remove an object from the database.
• remove Remove the given document based on one of the following: a specific object instance, a
query document criteria combined with a class or a query document criteria combined with a specific
collection name.
GeoSpatial queries are also supported and are described more in the section GeoSpatial Queries.
Map-Reduce operations are also supported and are described more in the section Map-Reduce.
All find methods take a Query object as a parameter. This object defines the criteria and options used
to perform the query. The criteria is specified using a Criteria object that has a static factory method
named where used to instantiate a new Criteria object. We recommend using a static import for
org.springframework.data.mongodb.core.query.Criteria.where and Query.query to
make the query more readable.
This query should return a list of Person objects that meet the specified criteria. The Criteria class
has the following methods that correspond to the operators provided in MongoDB.
As you can see most methods return the Criteria object to provide a fluent style for the API.
• Criteria and (String key) Adds a chained Criteria with the specified key to the current
Criteria and returns the newly created one
• Criteria in (Object... o) Creates a criterion using the $in operator for a varargs argument.
• Criteria in (Collection<?> collection) Creates a criterion using the $in operator using
a collection
• Criteria mod (Number value, Number remainder)Creates a criterion using the $mod
operator
• Criteria norOperator (Criteria... criteria)Creates an nor query using the $nor operator
for all of the provided criteria
• Criteria not ()Creates a criterion using the $not meta operator which affects the clause directly
following
There are also methods on the Criteria class for geospatial queries. Here is a listing but look at the
section on GeoSpatial Queries to see them in action.
• Criteria withinBox (Box box) Creates a geospatial criterion using a $within $box operation
• Criteria near (Point point) Creates a geospatial criterion using a $near operation
The Query class has some additional methods used to provide options for the query.
• Query addCriteria (Criteria criteria) used to add additional criteria to the query
• Query limit (int limit) used to limit the size of the returned results to the provided limit (used
for paging)
• Query skip (int skip) used to skip the provided number of documents in the results (used for
paging)
The query methods need to specify the target type T that will be returned and they are also overloaded
with an explicit collection name for queries that should operate on a collection other than the one
indicated by the return type.
• findOne Map the results of an ad-hoc query on the collection to a single instance of an object of the
specified type.
• find Map the results of an ad-hoc query on the collection to a List of the specified type.
• findAndRemove Map the results of an ad-hoc query on the collection to a single instance of an object
of the specified type. The first document that matches the query is returned and also removed from
the collection in the database.
GeoSpatial Queries
MongoDB supports GeoSpatial queries through the use of operators such as $near, $within, and
$nearSphere. Methods specific to geospatial queries are available on the Criteria class. There are
also a few shape classes, Box, Circle, and Point that are used in conjunction with geospatial related
Criteria methods.
To understand how to perform GeoSpatial queries we will use the following Venue class taken from the
integration tests.which relies on using the rich MappingMongoConverter.
@Document(collection="newyork")
public class Venue {
@Id
private String id;
private String name;
private double[] location;
@PersistenceConstructor
Venue(String name, double[] location) {
super();
this.name = name;
this.location = location;
}
@Override
public String toString() {
return "Venue [id=" + id + ", name=" + name + ", location="
+ Arrays.toString(location) + "]";
}
}
To find venues within a Circle using spherical coordinates the following query can be used
To find venues near a Point using spherical coordines the following query can be used
MongoDB supports querying the database for geo locations and calculation the distance from a given
origin at the very same time. With geo-near queries it's possible to express queries like: "find all
restaurants in the surrounding 10 miles". To do so MongoOperations provides geoNear(…) methods
taking a NearQuery as argument as well as the already familiar entity type and collection
As you can see we use the NearQuery builder API to set up a query to return all Restaurant instances
surrounding the given Point by 10 miles maximum. The Metrics enum used here actually implements
an interface so that other metrics could be plugged into a distance as well. A Metric is backed by a
multiplier to transform the distance value of the given metric into native distances. The sample shown
here would consider the 10 to be miles. Using one of the pre-built in metrics (miles and kilometers) will
automatically trigger the spherical flag to be set on the query. If you want to avoid that, simply hand
in plain double values into maxDistance(…). For more information see the JavaDoc of NearQuery
and Distance.
The geo near operations return a GeoResults wrapper object that encapsulates GeoResult
instances. The wrapping GeoResults allows to access the average distance of all results. A single
GeoResult object simply carries the entity found plus its distance from the origin.
Spring provides integration with MongoDB's map reduce by providing methods on MongoOperations
to simplify the creation and execution of Map-Reduce operations. It can convert the results of a Map-
Reduce operation to a POJO also integrates with Spring's Resource abstraction abstraction. This
will let you place your JavaScript files on the file system, classpath, http server or any other Spring
Resource implementation and then reference the JavaScript resources via an easy URI style syntax,
e.g. 'classpath:reduce.js;. Externalizing JavaScript code in files is often preferable to embedding them
as Java strings in your code. Note that you can still pass JavaScript code as Java strings if you prefer.
Example Usage
To understand how to perform Map-Reduce operations an example from the book 'MongoDB - The
definitive guide' is used. In this example we will create three documents that have the values [a,b], [b,c],
and [c,d] respectfully. The values in each document are associated with the key 'x' as shown below. For
this example assume these documents are in the collection named "jmr1".
A map function that will count the occurance of each letter in the array for each document is shown below
function () {
for (var i = 0; i < this.x.length; i++) {
emit(this.x[i], 1);
}
}
The reduce function that will sum up the occurance of each letter across all the documents is shown
below
Assuming that the map and reduce functions are located in map.js and reduce.js and bundled in your
jar so they are available on the classpath, you can execute a map-reduce operation and obtain the
results as shown below
MapReduceResults<ValueObject> results =
mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",
ValueObject.class);
for (ValueObject valueObject : results) {
System.out.println(valueObject);
}
The MapReduceResults class implements Iterable and provides access to the raw output, as well
as timing and count statistics. The ValueObject class is simply
@Override
public String toString() {
return "ValueObject [id=" + id + ", value=" + value + "]";
}
}
By default the output type of INLINE is used so you don't have to specify an output collection. To specify
additional map-reduce options use an overloaded method that takes an additional MapReduceOptions
argument. The class MapReduceOptions has a fluent API so adding additional options can be done in
a very compact syntax. Here an example that sets the output collection to "jmr1_out". Note that setting
only the output collection assumes a default output type of REPLACE.
MapReduceResults<ValueObject> results =
mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",
new
MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);
MapReduceResults<ValueObject> results =
mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",
options().outputCollection("jmr1_out"), ValueObject.class);
You can also specify a query to reduce the set of data that will be used to feed into the map-
reduce operation. This will remove the document that contains [a,b] from consideration for map-reduce
operations.
options().outputCollection("jmr1_out"), ValueObject.class);
Note that you can specify additional limit and sort values as well on the query but not skip values.
Map-Reduce. Using the group operations does have some limitations, for example it is not supported
in a shareded environment and it returns the full result set in a single BSON object, so the result should
be small, less than 10,000 keys.
Spring provides integration with MongoDB's group operation by providing methods on MongoOperations
to simplify the creation and execution of group operations. It can convert the results of the group
operation to a POJO and also integrates with Spring's Resource abstraction abstraction. This will
let you place your JavaScript files on the file system, classpath, http server or any other Spring
Resource implementation and then reference the JavaScript resources via an easy URI style syntax,
e.g. 'classpath:reduce.js;. Externalizing JavaScript code in files if often preferable to embedding them
as Java strings in your code. Note that you can still pass JavaScript code as Java strings if you prefer.
Example Usage
In order to understand how group operations work the following example is used, which is somewhat
artificial. For a more realistic example consult the book 'MongoDB - The definitive guide'. A collection
named "group_test_collection" created with the following rows.
We would like to group by the only field in each row, the 'x' field and aggregate the number of times
each specific value of 'x' occurs. To do this we need to create an initial document that contains our count
variable and also a reduce function which will increment it each time it is encountered. The Java code
to execute the group operation is shown below
The first argument is the name of the collection to run the group operation over, the second is a fluent
API that specifies properties of the group operation via a GroupBy class. In this example we are using
just the intialDocument and reduceFunction methods. You can also specify a key-function, as
well as a finalizer as part of the fluent API. If you have multiple keys to group by, you can pass in a
comma separated list of keys.
The raw results of the group operation is a JSON document that looks like this
{
"retval" : [ { "x" : 1.0 , "count" : 2.0} ,
{ "x" : 2.0 , "count" : 1.0} ,
{ "x" : 3.0 , "count" : 3.0} ] ,
"count" : 6.0 ,
"keys" : 3 ,
"ok" : 1.0
}
The document under the "retval" field is mapped onto the third argument in the group method, in this
case XObject which is shown below.
private float x;
@Override
public String toString() {
return "XObject [x=" + x + " count = " + count + "]";
}
}
You can also obtain the raw result as a DbObject by calling the method getRawResults on the
GroupByResults class.
There is an additional method overload of the group method on MongoOperations which lets you
specify a Criteria object for selecting a subset of the rows. An example which uses a Criteria
object, with some syntax sugar using static imports, as well as referencing a key-function and reduce
function javascript files via a Spring Resource string is shown below.
keyFunction("classpath:keyFunction.js").initialDocument("{ count:
0 }").reduceFunction("classpath:groupReduce.js"), XObject.class);
For further information see the full reference documentation of the aggregation framework and other
data aggregation tools for MongoDB.
Basic Concepts
The Aggregation Framework support in Spring Data MongoDB is based on the following key abstractions
Aggregation, AggregationOperation and AggregationResults.
• Aggregation
The actual aggregate operation is executed by the aggregate method of the MongoTemplate which
also takes the desired output class as parameter.
• AggregationOperation
• AggregationResults
AggregationResults is the container for the result of an aggregate operation. It provides access
to the raw aggregation result in the form of an DBObject, to the mapped objects and information
which performed the aggregation.
The canonical example for using the Spring Data MongoDB support for the MongoDB Aggregation
Framework looks as follows:
AggregationResults<OutputType> results =
mongoTemplate.aggregate(agg, "INPUT_COLLECTION_NAME", OutputType.class);
List<OutputType> mappedResult = results.getMappedResults();
Note that if you provide an input class as the first parameter to the newAggregation method the
MongoTemplate will derive the name of the input collection from this class. Otherwise if you don't not
specify an input class you must provide the name of the input collection explicitly. If an input-class and
an input-collection is provided the latter takes precedence.
The MongoDB Aggregation Framework provides the following types of Aggregation Operations:
At the time of this writing we provide support for the following Aggregation Operations in Spring Data
MongoDB.
Pipeline Aggregation Operators project, skip, limit, unwind, group, sort, geoNear
Group Aggregation Operators addToSet, first, last, max, min, avg, push, sum,
(*count)
Arithmetic Aggregation Operators add (*via plus), subtract (*via minus), multiply,
divide, mod
Note that the aggregation operations not listed here are currently not supported by Spring Data
MongoDB. Comparison aggregation operators are expressed as Criteria expressions.
Projection Expressions
Projection expressions are used to define the fields that are the outcome of a particular aggregation
step. Projection expressions can be defined via the project method of the Aggregate class.
Note that more examples for project operations can be found in the AggregationTests class.
Example 4.17 Projection expression examples
Note that further details regarding the projection expressions can be found in the corresponding section
of the MongoDB Aggregation Framework reference documentation.
As of Version 1.4.0 we support the use of SpEL expression in projection expressions via the
andExpression method of the ProjectionOperation class. This allows you to define the desired
expression as a SpEL expression which is translated into a corresponding MongoDB projection
expression part on query execution. This makes it much easier to express complex calculations.
1 + (q + 1) / (q - 1)
{ "$add" : [ 1, {
"$divide" : [ {
"$add":["$q", 1]}, {
"$subtract":[ "$q", 1]}
]
}]}
Have a look at an example in more context in Example 4.23, “Aggregation Framework Example 5” and
Example 4.24, “Aggregation Framework Example 6”. You can find more usage examples for supported
SpEL expression constructs in SpelExpressionTransformerUnitTests.
In this introductory example we want to aggregate a list of tags to get the occurrence count of a particular
tag from a MongoDB collection called "tags" sorted by the occurrence count in descending order.
This example demonstrates the usage of grouping, sorting, projections (selection) and unwinding (result
splitting).
class TagCount {
String tag;
int n;
}
• In order to do this we first create a new aggregation via the newAggregation static factory method
to which we pass a list of aggregation operations. These aggregate operations define the aggregation
pipeline of our Aggregation.
• As a second step we select the "tags" field (which is an array of strings) from the input collection
with the project operation.
• In a third step we use the unwind operation to generate a new document for each tag within the
"tags" array.
• In the forth step we use the group operation to define a group for each "tags"-value for which we
aggregate the occurrence count via the count aggregation operator and collect the result in a new
field called "n".
• As a fifth step we select the field "n" and create an alias for the id-field generated from the previous
group operation (hence the call to previousOperation()) with the name "tag".
• As the sixth step we sort the resulting list of tags by their occurrence count in descending order via
the sort operation.
• Finally we call the aggregate Method on the MongoTemplate in order to let MongoDB perform the
acutal aggregation operation with the created Aggregation as an argument.
Note that the input collection is explicitly specified as the "tags" parameter to the aggregate Method.
If the name of the input collection is not specified explicitly, it is derived from the input-class passed as
first parameter to the newAggreation Method.
This example is based on the Largest and Smallest Cities by State example from the MongoDB
Aggregation Framework documentation. We added additional sorting to produce stable results with
different MongoDB versions. Here we want to return the smallest and largest cities by population for
each state, using the aggregation framework. This example demonstrates the usage of grouping, sorting
and projections (selection).
class ZipInfo {
String id;
String city;
String state;
@Field("pop") int population;
@Field("loc") double[] location;
}
class City {
String name;
int population;
}
class ZipInfoStats {
String id;
String state;
City biggestCity;
City smallestCity;
}
• The class ZipInfo maps the structure of the given input-collection. The class ZipInfoStats
defines the structure in the desired output format.
• As a first step we use the group operation to define a group from the input-collection. The grouping
criteria is the combination of the fields "state" and "city" which forms the id structure of the
group. We aggregate the value of the "population" property from the grouped elements with by
using the sum operator saving the result in the field "pop".
• In a second step we use the sort operation to sort the intermediate-result by the fields "pop",
"state" and "city" in ascending order, such that the smallest city is at the top and the biggest
city is at the bottom of the result. Note that the sorting on "state" and "city" is implicitly performed
against the group id fields which Spring Data MongoDB took care of.
• In the third step we use a group operation again to group the intermediate result by "state".
Note that "state" again implicitly references an group-id field. We select the name and the
population count of the biggest and smallest city with calls to the last(…) and first(...) operator
respectively via the project operation.
• As the forth step we select the "state" field from the previous group operation. Note that "state"
again implicitly references an group-id field. As we do not want an implicit generated id to appear,
we exclude the id from the previous operation via and(previousOperation()).exclude(). As
we want to populate the nested City structures in our output-class accordingly we have to emit
appropriate sub-documents with the nested method.
• Finally as the fifth step we sort the resulting list of StateStats by their state name in ascending
order via the sort operation.
Note that we derive the name of the input-collection from the ZipInfo-class passed as first parameter
to the newAggregation-Method.
This example is based on the States with Populations Over 10 Million example from the MongoDB
Aggregation Framework documentation. We added additional sorting to produce stable results with
different MongoDB versions. Here we want to return all states with a population greater than 10 million,
using the aggregation framework. This example demonstrates the usage of grouping, sorting and
matching (filtering).
class StateStats {
@Id String id;
String state;
@Field("totalPop") int totalPopulation;
}
• As a first step we group the input collection by the "state" field and calculate the sum of the
"population" field and store the result in the new field "totalPop".
• In the second step we sort the intermediate result by the id-reference of the previous group operation
in addition to the "totalPop" field in ascending order.
• Finally in the third step we filter the intermediate result by using a match operation which accepts a
Criteria query as an argument.
Note that we derive the name of the input-collection from the ZipInfo-class passed as first parameter
to the newAggregation-Method.
This example demonstrates the use of simple arithmetic operations in the projection operation.
class Product {
String id;
String name;
double netPrice;
int spaceUnits;
}
Note that we derive the name of the input-collection from the Product-class passed as first parameter
to the newAggregation-Method.
This example demonstrates the use of simple arithmetic operations derived from SpEL Expressions in
the projection operation.
class Product {
String id;
String name;
double netPrice;
int spaceUnits;
}
);
This example demonstrates the use of complex arithmetic operations derived from SpEL Expressions
in the projection operation.
Note: The additional parameters passed to the addExpression Method can be referenced via indexer
expressions according to their position. In this example we reference the parameter shippingCosts
which is the first parameter of the parameters array via [0]. External parameter expressions are
replaced with their respective values when the SpEL expression is transformed into a MongoDB
aggregation framework expression.
class Product {
String id;
String name;
double netPrice;
int spaceUnits;
}
Note that we can also refer to other fields of the document within the SpEL expression.
The MappingMongoConverter checks to see if there are any Spring converters that can handle a
specific class before attempting to map the object itself. To 'hijack' the normal mapping strategies of
the MappingMongoConverter, perhaps for increased performance or other custom mapping needs,
you first need to create an implementation of the Spring Converter interface and then register it with
the MappingConverter.
Note
For more information on the Spring type conversion service see the reference docs here.
import org.springframework.core.convert.converter.Converter;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
An example implementation of a Converter that converts from a DBObject ot a Person object is shownn
below
The Mongo Spring namespace provides a convenience way to register Spring Converters with the
MappingMongoConverter. The configuration snippet below shows how to manually register converter
beans as well as configuring the wrapping MappingMongoConverter into a MongoTemplate.
<mongo:db-factory dbname="database"/>
<mongo:mapping-converter>
<mongo:custom-converters>
<mongo:converter ref="readConverter"/>
<mongo:converter>
<bean class="org.springframework.data.mongodb.test.PersonWriteConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
You can also use the base-package attribute of the custom-converters element to enable classpath
scanning for all Converter and GenericConverter implementations below the given package.
<mongo:mapping-converter>
<mongo:custom-converters base-package="com.acme.**.converters" />
</mongo:mapping-converter>
Converter disambiguation
Generally we inspect the Converter implementations for the source and target types they convert from
and to. Depending on whether one of those is a type MongoDB can handle natively we will register the
converter instance as reading or writing one. Have a look at the following samples:
// Write converter as only the target type is one Mongo can handle natively
class MyConverter implements Converter<Person, String> { … }
// Read converter as only the source type is one Mongo can handle natively
class MyConverter implements Converter<String, Person> { … }
In case you write a Converter whose source and target type are native Mongo types there's no
way for us to determine whether we should consider it as reading or writing converter. Registering the
converter instance as both might lead to unwanted results then. E.g. a Converter<String, Long>
is ambiguous although it probably does not make sense to try to convert all Strings into Longs when
writing. To be generally able to force the infrastructure to register a converter for one way only we provide
@ReadingConverter as well as @WritingConverter to be used at the converter implementation.
void dropAllIndexes();
void resetIndexCache();
List<IndexInfo> getIndexInfo();
}
mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING));
• ensureIndex Ensure that an index for the provided IndexDefinition exists for the collection.
You can create both standard indexes and geospatial indexes using the classes IndexDefinition
and GeoSpatialIndex respectfully. For example, given the Venue class defined in a previous section,
you would declare a geospatial query as shown below
mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));
The IndexOperations interface has the method getIndexInfo that returns a list of IndexInfo objects. This
contains all the indexes defined on the collectcion. Here is an example that defines an index on the
Person class that has age property.
template.indexOps(Person.class).ensureIndex(new Index().on("age",
Order.DESCENDING).unique(Duplicates.DROP));
// Contains
// [IndexInfo [fieldSpec={_id=ASCENDING}, name=_id_, unique=false, dropDuplicates=false,
sparse=false],
// IndexInfo [fieldSpec={age=DESCENDING}, name=age_-1, unique=true, dropDuplicates=true,
sparse=false]]
It's time to look at some code examples showing how to use the MongoTemplate. First we look at
creating our first collection.
mongoTemplate.dropCollection("MyNewCollection");
To intercept an object before it goes through the conversion process (which turns your domain object
into a com.mongodb.DBObject), you'd register a subclass of AbstractMongoEventListener that
overrides the onBeforeConvert method. When the event is dispatched, your listener will be called
and passed the domain object before it goes into the converter.
Example 4.27
To intercept an object before it goes into the database, you'd register a subclass of
org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener
that overrides the onBeforeSave method. When the event is dispatched, your listener will be called
and passed the domain object and the converted com.mongodb.DBObject.
Example 4.28
Simply declaring these beans in your Spring ApplicationContext will cause them to be invoked whenever
the event is dispatched.
• onBeforeConvert - called in MongoTemplate insert, insertList and save operations before the
object is converted to a DBObject using a MongoConveter.
• onBeforeSave - called in MongoTemplate insert, insertList and save operations before inserting/
saving the DBObject in the database.
• onAfterSave - called in MongoTemplate insert, insertList and save operations after inserting/saving
the DBObject in the database.
The motivation behind mapping to Spring's consistent data access exception hierarchy is that you
are then able to write portable and descriptive exception handling code without resorting to coding
against MongoDB error codes. All of Spring's data access exceptions are inherited from the root
DataAccessException class so you can be sure that you will be able to catch all database related
exception within a single try-catch block. Note, that not all exceptions thrown by the MongoDB driver
inherit from the MongoException class. The inner exception and message are preserved so no
information is lost.
• <T> T executeInSession (DbCallback<T> action) Executes the given DbCallback within the
same connection to the database so as to ensure consistency in a write heavy environment where
you may read the data that you wrote.
Here is an example that uses the CollectionCallback to return information about an index
@Bean
public GridFsTemplate gridFsTemplate() {
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
}
}
<bean class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
<constructor-arg ref="mongoDbFactory" />
<constructor-arg ref="converter" />
</bean>
</beans>
The template can now be injected and used to perform storage and retrieval operations.
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void storeFileToGridFs {
The store(…) operations take an InputStream, a filename and optionally metadata information
about the file to store. The metadata can be an arbitrary object which will be marshalled by the
MongoConverter configured with the GridFsTemplate. Alternatively you can also provide a
DBObject as well.
Reading files from the filesystem can either be achieved through the find(…) or getResources(…)
methods. Let's have a look at the find(…) methods first. You can either find a single file matching a
Query or multiple ones. To easily define file queries we provide the GridFsCriteria helper class.
It provides static factory methods to encapsulate default metadata fields (e.g. whereFilename(),
whereContentType()) or the custom one through whereMetaData().
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void findFilesInGridFs {
List<GridFSDBFile> result = operations.find(query(whereFilename().is("filename.txt")))
}
}
Note
Currently MongoDB does not support defining sort criterias when retrieving files from GridFS.
Thus any sort criterias defined on the Query instance handed into the find(…) method will be
disregarded.
The other option to read files from the GridFs is using the methods introduced by the
ResourcePatternResolver interface. They allow handing an Ant path into the method ar thus
retrieve files matching the given pattern.
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void readFilesFromGridFs {
GridFsResources[] txtFiles = operations.getResources("*.txt");
}
}
5. MongoDB repositories
5.1 Introduction
This chapter will point out the specialties for repository support for MongoDB. This builds on the core
repository support explained in Chapter 3, Working with Spring Data Repositories. So make sure you've
got a sound understanding of the basic concepts explained there.
5.2 Usage
To access domain entities stored in a MongoDB you can leverage our sophisticated repository support
that eases implementing those quite significantly. To do so, simply create an interface for your repository:
@Id
private String id;
private String firstname;
private String lastname;
private Address address;
We have a quite simple domain object here. Note that it has a property named id of typeObjectId.
The default serialization mechanism used in MongoTemplate (which is backing the repository
support) regards properties named id as document id. Currently we supportString, ObjectId and
BigInteger as id-types.
Right now this interface simply serves typing purposes but we will add additional methods to it later. In
your Spring configuration simply add
</beans>
This namespace element will cause the base packages to be scanned for interfaces extending
MongoRepository and create Spring beans for each of them found. By default the repositories will get
a MongoTemplate Spring bean wired that is called mongoTemplate, so you only need to configure
mongo-template-ref explicitly if you deviate from this convention.
If you'd rather like to go with JavaConfig use the @EnableMongoRepositories annotation. The
annotation carries the very same attributes like the namespace element. If no base package is
configured the infrastructure will scan the package of the annotated configuration class.
@Configuration
@EnableMongoRepositories
class ApplicationConfig extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "e-store";
}
@Override
public Mongo mongo() throws Exception {
return new Mongo();
}
@Override
protected String getMappingBasePackage() {
return "com.oreilly.springdata.mongodb"
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PersonRepositoryTests {
@Test
public void readsFirstPageCorrectly() {
The sample creates an application context with Spring's unit test support which will perform annotation
based dependency injection into test cases. Inside the test method we simply use the repository to
query the datastore. We hand the repository a PageRequest instance that requests the first page of
persons at a page size of 10.
The first method shows a query for all people with the given lastname. The query will be derived parsing
the method name for constraints which can be concatenated with And and Or. Thus the method name
will result in a query expression of{"lastname" : lastname}. The second example shows how
pagination is applied to a query. Just equip your method signature with a Pageable parameter and
let the method return a Page instance and we will automatically page the query accordingly. The third
examples shows that you can query based on properties which are not a primitive type.
Note
Note that for version 1.0 we currently don't support referring to parameters that are mapped as
DBRef in the domain class.
GreaterThanEqual
findByAgeGreaterThanEqual(int {"age" : {"$gte" : age}}
age)
As you've just seen there are a few keywords triggering geo-spatial operations within a MongoDB query.
The Near keyword allows some further modification. Let's have look at some examples:
Adding a Distance parameter to the query method allows restricting results to those within the given
distance. If the Distance was set up containing a Metric we will transparently use $nearSphere
instead of $code.
As you can see using a Distance equipped with a Metric causes $nearSphere clause to be added
instead of a plain $near. Beyond that the actual distance gets calculated according to the Metrics
used.
Geo-near queries
The placeholder ?0 lets you substitute the value from the method arguments into the JSON query string.
You can also use the filter property to restrict the set of properties that will be mapped into the Java
object. For example,
This will return only the firstname, lastname and Id properties of the Person objects. The age property,
a java.lang.Integer, will not be set and its value will therefore be null.
• Code completion in IDE (all properties, methods and operations can be expanded in your favorite
Java IDE)
• Domain types and properties can be referenced safely (no Strings involved!)
Please refer to the QueryDSL documentation which describes how to bootstrap your environment for
APT based code generation using Maven or using Ant.
QPerson is a class that is generated (via the Java annotation post processing tool) which is a
Predicate that allows you to write type safe queries. Notice that there are no strings in the query other
than the value "C0123".
You can use the generated Predicate class via the interface QueryDslPredicateExecutor which
is shown below
T findOne(Predicate predicate);
To use this in your repository implementation, simply inherit from it in addition to other repository
interfaces. This is shown below
We think you will find this an extremely powerful tool for writing MongoDB queries.
5.4 Miscellaneous
CDI Integration
Instances of the repository interfaces are usually created by a container, which Spring is the most natural
choice when working with Spring Data. As of version 1.3.0 Spring Data MongoDB ships with a custom
CDI extension that allows using the repository abstraction in CDI environments. The extension is part of
the JAR so all you need to do to activate it is dropping the Spring Data MongoDB JAR into your classpath.
You can now set up the infrastructure by implementing a CDI Producer for the MongoTemplate:
class MongoTemplateProducer {
@Produces
@ApplicationScoped
public MongoOperations createMongoTemplate() throws UnknownHostException,
MongoException {
The Spring Data MongoDB CDI extension will pick up the MongoTemplate available as CDI bean and
create a proxy for a Spring Data repository whenever an bean of a repository type is requested by the
container. Thus obtaining an instance of a Spring Data repository is a matter of declaring an @Inject-
ed property:
class RepositoryClient {
@Inject
PersonRepository repository;
6. Mapping
Rich mapping support is provided by the MongoMappingConverter. MongoMappingConverter
has a rich metadata model that provides a full feature set of functionality to map domain objects to
MongoDB documents.The mapping metadata model is populated using annotations on your domain
objects. However, the infrastructure is not limited to using annotations as the only source of metadata
information. The MongoMappingConverter also allows you to map objects to documents without
providing any additional metadata, by following a set of conventions.
In this section we will describe the features of the MongoMappingConverter. How to use conventions for
mapping objects to documents and how to override those conventions with annotation based mapping
metadata.
Note
SimpleMongoConverter has been deprecated in Spring Data MongoDB M3 as all of its
functionality has been subsumed into MappingMongoConverter.
• The short Java class name is mapped to the collection name in the following manner. The class
'com.bigbank.SavingsAccount' maps to 'savingsAccount' collection name.
• All nested objects are stored as nested objects in the document and *not* as DBRefs
• The converter will use any Spring Converters registered with it to override the default mapping of
object properties to document field/values.
• The fields of an object are used to convert to and from fields in the document. Public JavaBean
properties are not used.
• You can have a single non-zero argument constructor whose constructor argument names match top
level field names of document, that constructor will be used. Otherwise the zero arg constructor will
be used. if there is more than one non-zero argument constructor an exception will be thrown.
MongoDB requires that you have an '_id' field for all documents. If you don't provide one the driver will
assign a ObjectId with a generated value. The "_id" field can be of any type the, other than arrays,
so long as it is unique. The driver naturally supports all primitive types and Dates. When using the
MongoMappingConverter there are certain rules that govern how properties from the Java class is
mapped to this '_id' field.
The following outlines what field will be mapped to the '_id' document field:
• A field without an annotation but named id will be mapped to the '_id' field.
The following outlines what type conversion, if any, will be done on the property mapped to the _id
document field.
• If a field named 'id' is declared as a String or BigInteger in the Java class it will be converted to and
stored as an ObjectId if possible. ObjectId as a field type is also valid. If you specify a value for 'id' in
your application, the conversion to an ObjectId is detected to the MongoDBdriver. If the specified 'id'
value cannot be converted to an ObjectId, then the value will be stored as is in the document's _id field.
• If a field named ' id' id field is not declared as a String, BigInteger, or ObjectID in the Java class then
you should assign it a value in your application so it can be stored 'as-is' in the document's _id field.
• If no field named 'id' is present in the Java class then an implicit '_id' file will be generated by the driver
but not mapped to a property or field of the Java class.
When querying and updating MongoTemplate will use the converter to handle conversions of the
Query and Update objects that correspond to the above rules for saving documents so field names
and types used in your queries will be able to match what is in your domain classes.
@Configuration
public class GeoSpatialAppConfig extends AbstractMongoConfiguration {
@Bean
public Mongo mongo() throws Exception {
return new Mongo("localhost");
}
@Override
public String getDatabaseName() {
return "database";
}
@Override
public String getMappingBasePackage() {
return "com.bigbank.domain";
}
@Bean
@Override
public CustomConversions customConversions() throws Exception {
List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
converterList.add(new org.springframework.data.mongodb.test.PersonReadConverter());
converterList.add(new org.springframework.data.mongodb.test.PersonWriteConverter());
return new CustomConversions(converterList);
}
@Bean
public LoggingEventListener<MongoMappingEvent> mappingEventsListener() {
return new LoggingEventListener<MongoMappingEvent>();
}
You can add additional converters to the converter by overriding the method
afterMappingMongoConverterCreation. Also shown in the above example is a
LoggingEventListener which logs MongoMappingEvents that are posted onto Spring's
ApplicationContextEvent infrastructure.
Note
AbstractMongoConfiguration will create a MongoTemplate instance and registered with the
container under the name 'mongoTemplate'.
You can also override the method UserCredentials getUserCredentials() to provide the
username and password information to connect to the database.
Spring's MongoDB namespace enables you to easily enable mapping functionality in XML
<!-- by default look for a Mongo object named 'mongo' - default name used for the
converter is 'mappingConverter' -->
<mongo:mapping-converter base-package="com.bigbank.domain">
<mongo:custom-converters>
<mongo:converter ref="readConverter"/>
<mongo:converter>
<bean class="org.springframework.data.mongodb.test.PersonWriteConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener"/>
</beans
The base-package property tells it where to scan for classes annotated with the
@org.springframework.data.mongodb.core.mapping.Document annotation.
package com.mycompany.domain;
@Document
public class Person {
@Id
private ObjectId id;
@Indexed
private Integer ssn;
@Indexed
private String lastName;
Important
The @Id annotation tells the mapper which property you want to use for the MongoDB _id
property and the @Indexed annotation tells the mapping framework to call ensureIndex on
that property of your document, making searches faster.
The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An
overview of the annotations is provided below
• @Id - applied at the field level to mark the field used for identiy purpose.
• @Document - applied at the class level to indicate this class is a candidate for mapping to the
database. You can specify the name of the collection where the database will be stored.
• @Indexed - applied at the field level to describe how to index the field.
• @GeoSpatialIndexed - applied at the field level to describe how to geoindex the field.
• @Transient - by default all private fields are mapped to the document, this annotation excludes the
field where it is applied from being stored in the database
• @Value - this annotation is part of the Spring Framework . Within the mapping framework it can
be applied to constructor arguments. This lets you use a Spring Expression Language statement
to transform a key's value retrieved in the database before it is used to construct a domain
object. In order to reference a property of a given document one has to use expressions like:
@Value("#root.myProperty") where root refers to the root of the given document.
• @Field - applied at the field level and described the name of the field as it will be represented in the
MongoDB BSON document thus allowing the name to be different than the fieldname of the class.
@Document
@CompoundIndexes({
@CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}")
})
public class Person<T extends Address> {
@Id
private String id;
@Indexed(unique = true)
private Integer ssn;
@Field("fName")
private String firstName;
@Indexed
private String lastName;
@Transient
private Integer accountTotal;
@DBRef
private List<Account> accounts;
private T address;
@PersistenceConstructor
public Person(Integer ssn, String firstName, String lastName, Integer age, T address) {
this.ssn = ssn;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.address = address;
}
// no setter for Id. (getter is only exposed for some unit testing)
• If a parameter is annotated with the @Value annotation, the given expression is evaluated and the
result is used as the parameter value.
• If the Java type has a property whose name matches the given field of the input document, then it's
property information is used to select the appropriate constructor parameter to pass the input field
value to. This works only if the parameter name information is present in the java .class files which
can be achieved by compiling the source with debug information or using the new -parameters
command-line switch for javac in Java 8.
• Otherwise an MappingException will be thrown indicating that the given constructor parameter
could not be bound.
class OrderItem {
// getters/setters ommitted
}
Note
The SpEL expression in the @Value annotation of the quantity parameter falls back to the
value 0 if the given property path cannot be resolved.
Additional examples for using the @PersistenceConstructor annotation can be found in the
MappingMongoConverterUnitTests test suite.
Compound Indexes
Compound indexes are also supported. They are defined at the class level, rather than on indidvidual
properties.
Note
Compound indexes are very important to improve the performance of queries that involve criteria
on multiple fields
Here's an example that creates a compound index of lastName in ascending order and age in
descending order:
package com.mycompany.domain;
@Document
@CompoundIndexes({
@CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}")
})
public class Person {
@Id
private ObjectId id;
private Integer age;
private String firstName;
private String lastName;
Using DBRefs
The mapping framework doesn't have to store child objects embedded within the document. You can
also store them separately and use a DBRef to refer to that document. When the object is loaded from
MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks
the same as if it had been stored embedded within your master document.
Here's an example of using a DBRef to refer to a specific document that exists independently of the
object in which it is referenced (both classes are shown in-line for brevity's sake):
@Document
public class Account {
@Id
private ObjectId id;
private Float total;
@Document
public class Person {
@Id
private ObjectId id;
@Indexed
private Integer ssn;
@DBRef
private List<Account> accounts;
Example 6.5
There's no need to use something like @OneToMany because the mapping framework sees that you're
wanting a one-to-many relationship because there is a List of objects. When the object is stored in
MongoDB, there will be a list of DBRefs rather than the Account objects themselves.
Important
The mapping framework does not handle cascading saves. If you change an Account object that
is referenced by a Person object, you must save the Account object separately. Calling save on
the Person object will not automatically save the Account objects in the property accounts.
Events are fired throughout the lifecycle of the mapping process. This is described in the Lifecycle
Events section.
Simply declaring these beans in your Spring ApplicationContext will cause them to be invoked whenever
the event is dispatched.
When storing and querying your objects it is convenient to have a MongoConverter instance
handle the mapping of all Java types to DBObjects. However, sometimes you may want the
MongoConverter's do most of the work but allow you to selectivly handle the conversion for a particular
type or to optimize performance.
Note
Spring 3.0 introduced a core.convert package that provides a general type conversion system.
This is described in detail in the Spring reference documentation section entitled Spring 3 Type
Conversion.
Below is an example of a Spring Converter implementation that converts from a DBObject to a Person
POJO.
@ReadingConverter
public class PersonReadConverter implements Converter<DBObject, Person> {
@WritingConverter
public class PersonWriteConverter implements Converter<Person, DBObject> {
...
...
</project>
Once this is done we need to enable AspectJ for the project. The cross-store support is implemented
using AspectJ aspects so by enabling compile time AspectJ support the cross-store features will become
available to your project. In Maven you would add an additional plugin to the <build> section of the pom:
...
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<!-- NB: You must use Maven 2.0.9 or above or these are ignored (see MNG-2972)
-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-cross-store</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Spring Data MongoDB -
Example 7.2 Example
1.4.2.RELEASE Maven pom.xml with AspectJ
Reference plugin enabled
Documentation 90
Spring Data MongoDB - Reference Documentation
Finally, you need to configure your project to use MongoDB and also configure the aspects that are
used. The following XML snippet should be added to your application context:
...
<bean class="org.springframework.data.mongodb.core.MongoExceptionTranslator"/>
...
</beans>
Example 7.3 Example application context with MongoDB and cross-store aspect support
completion, loading the document from MongoDB the first time the value is used in your application.
Here is an example of a simple Entity that has a field annotated with @RelatedEntity.
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@RelatedDocument
private SurveyInfo surveyInfo;
public SurveyInfo() {
this.questionsAndAnswers = new HashMap<String, String>();
}
Once the SurveyInfo has been set on the Customer object above the MongoTemplate that was
configured above is used to save the SurveyInfo along with some metadata about the JPA Entity is stored
in a MongoDB collection named after the fully qualified name of the JPA Entity class. The following code:
Example 7.6 Example of code using the JPA Entity configured for cross-store persistence
Executing the code above results in the following JSON document stored in MongoDB.
8. Logging support
An appender for Log4j is provided in the maven module "spring-data-mongodb-log4j". Note, there is no
dependency on other Spring Mongo modules, only the MongoDB driver.
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.springframework.data.document.mongodb.log4j.MongoLog4jAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.stdout.host = localhost
log4j.appender.stdout.port = 27017
log4j.appender.stdout.database = logs
log4j.appender.stdout.collectionPattern = %X{year}%X{month}
log4j.appender.stdout.applicationId = my.application
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
log4j.category.org.apache.activemq=ERROR
log4j.category.org.springframework.batch=DEBUG
log4j.category.org.springframework.data.document.mongodb=DEBUG
log4j.category.org.springframework.transaction=INFO
The important configuration to look at aside from host and port is the database and collectionPattern.
The variables year, month, day and hour are available for you to use in forming a collection name. This
is to support the common convention of grouping log information in a collection that corresponds to a
specific time period, for example a collection per day.
There is also an applicationId which is put into the stored message. The document stored from logging
as the following keys: level, name, applicationId, timestamp, properties, traceback, and message.
9. JMX support
The JMX support for MongoDB exposes the results of executing the 'serverStatus' command on the
admin database for a single MongoDB server instance. It also exposes an administrative MBean,
MongoAdmin which will let you perform administrative operations such as drop or create a database.
The JMX features build upon the JMX feature set available in the Spring Framework. See here for
more details.
<beans>
<context:mbean-export/>
</beans>
• AssertMetrics
• BackgroundFlushingMetrics
• BtreeIndexCounters
• ConnectionMetrics
• GlobalLoclMetrics
• MemoryMetrics
• OperationCounters
• ServerInfo
• MongoAdmin
Name Description
named-queries-location Defines the location to look for a Properties file containing externally
defined queries.
1
see the section called “XML configuration”
AND And
OR Or
EXISTS Exists
GREATER_THAN_EQUALS
GreaterThanEqual, IsGreaterThanEqual
IN In, IsIn
LESS_THAN_EQUALLessThanEqual, IsLessThanEqual