© 2008-2022 The original authors.
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. |
Preface
The Spring Data MongoDB project applies core Spring concepts to the development of solutions that use the MongoDB document style data store. We provide a “template” as a high-level abstraction for storing and querying documents. You may notice similarities to the JDBC support provided by the Spring Framework.
This document is the reference guide for Spring Data - MongoDB Support. It explains MongoDB module concepts and semantics and syntax for various store namespaces.
This section provides some basic introduction to Spring and Document databases. The rest of the document refers only to Spring Data MongoDB features and assumes the user is familiar with MongoDB and Spring concepts.
1. Learning Spring
Spring Data uses Spring framework’s core functionality, including:
While you need not know the Spring APIs, understanding the concepts behind them is important. At a minimum, the idea behind Inversion of Control (IoC) should be familiar, and you should be familiar with whatever IoC container you choose to use.
The core functionality of the MongoDB 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 MongoDB, such as the repository support, you need to configure some parts of the library to use Spring.
To learn more about Spring, you can refer to the comprehensive documentation that explains the Spring Framework in detail. There are a lot of articles, blog entries, and books on the subject. See the Spring framework home page for more information.
2. Learning NoSQL and Document databases
NoSQL stores have taken the storage world by storm. It is a vast domain with a plethora of solutions, terms, and patterns (to make things worse, even the term itself has multiple meanings). While some of the principles are common, you must be familiar with MongoDB to some degree. The best way to get acquainted is to read the documentation and follow the examples. It usually does not take more then 5-10 minutes to go through them and, especially if you are coming from an RDMBS-only background, these exercises can be an eye opener.
The starting point 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.
-
MongoDB Java Language Center.
-
Several books you can purchase.
-
Karl Seguin’s online book: The Little MongoDB Book.
3. Requirements
The Spring Data MongoDB 3.x binaries require JDK level 8.0 and above and Spring Framework 5.3.22 and above.
In terms of document stores, you need at least version 3.6 of MongoDB, though we recommend a more recent version.
3.1. Compatibility Matrix
The following compatibility matrix summarizes Spring Data versions to MongoDB driver/database versions. Database versions show the highest supported server version that pass the Spring Data test suite. You can use newer server versions unless your application uses functionality that is affected by changes in the MongoDB server. See also the official MongoDB driver compatibility matrix for driver- and server version compatibility.
Spring Data Release Train | Spring Data MongoDB | Driver Version | Server Version |
---|---|---|---|
2021.1 |
|
|
|
2021.0 |
|
|
|
2020.0 |
|
|
|
Neumann |
|
|
|
Moore |
|
|
|
Lovelace |
|
|
|
3.1.1. Relevant Changes in MongoDB 4.4
-
Fields list must not contain text search score property when no
$text
criteria present. See also$text
operator -
Sort must not be an empty document when running map reduce.
3.1.2. Relevant Changes in MongoDB 4.2
-
Removal of
geoNear
command. See also Removal ofgeoNear
-
Removal of
eval
command. See also Removal ofeval
4. Additional Help Resources
Learning a new framework is not always straightforward. In this section, we try to provide what we think is an easy-to-follow guide for starting with the Spring Data MongoDB module. However, if you encounter issues or you need advice, feel free to use one of the following links:
- Community Forum
-
Spring Data on Stack Overflow is a tag 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 Software, Inc., the company behind Spring Data and Spring.
5. Following Development
For information on the Spring Data Mongo source code repository, nightly builds, and snapshot artifacts, see the Spring Data Mongo homepage. You can help make Spring Data best serve the needs of the Spring community by interacting with developers through the Community on Stack Overflow. 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. You can also follow the Spring blog or the project team on Twitter (SpringData).
6. New & Noteworthy
6.1. What’s New in Spring Data MongoDB 3.3
-
Extended support for referencing entities.
-
Support for Time Series collections.
-
Include/exclude
null
properties on write toDocument
through@Field(write=…)
. -
Support for Wildcard Indexes.
6.2. What’s New in Spring Data MongoDB 3.2
-
Support for unwrapping nested objects into the parent
Document
.
6.3. What’s New in Spring Data MongoDB 3.1
-
Reactive auditing enabled through
@EnableReactiveMongoAuditing
.@EnableMongoAuditing
no longer registersReactiveAuditingEntityCallback
. -
Reactive SpEL support in
@Query
and@Aggregation
query methods. -
Aggregation hints via
AggregationOptions.builder().hint(bson).build()
. -
Extension Function
KProperty.asPath()
to render property references into a property path representation. -
Server-side JavaScript aggregation expressions
$function
and$accumulator
viaScriptOperators
.
6.4. What’s New in Spring Data MongoDB 3.0
-
Upgrade to MongoDB Driver 4.0. See Upgrading from 2.x to 3.x for further details.
-
Auto-index creation is now disabled by default.
-
Support for aggregation pipelines in update operations.
-
Removal of
_id
flattening for composite Id’s when usingMongoTemplate
aggregations. -
Apply pagination when using GridFS
find(Query)
. -
Sharding key derivation via
@Sharded
. -
$merge
and$addFields
aggregation pipeline stages. -
@TextScore
isnull
for entities retrieved through non-fulltext queries.
6.5. What’s New in Spring Data MongoDB 2.2
-
Compatibility with MongoDB 4.2 deprecating
eval
,group
andgeoNear
Template API methods. -
Extended SpEL aggregation support for MongoDB 3.4 and MongoDB 4.0 operators (see Spring Expression Support in Projection Expressions).
-
Querydsl support for reactive repositories via
ReactiveQuerydslPredicateExecutor
. -
Aggregation framework support via repository query methods.
-
Declarative reactive transactions using @Transactional.
-
Template API delete by entity considers the version property in delete queries.
-
Repository deletes now throw
OptimisticLockingFailureException
when a versioned entity cannot be deleted. -
Support
Range<T>
in repository between queries. -
Changed behavior of
Reactive/MongoOperations#count
now limiting the range to count matches within by passing on offset & limit to the server. -
Support of array filters in
Update
operations. -
JSON Schema generation from domain types.
-
SpEL support in for expressions in
@Indexed
. -
Support for Hashed Indexes.
-
Annotation-based Collation support through
@Document
and@Query
. -
Kotlin extension methods accepting
KClass
are deprecated now in favor ofreified
methods. -
Kotlin Coroutines support.
6.6. What’s New in Spring Data MongoDB 2.1
-
Cursor-based aggregation.
-
Distinct queries for imperative and reactive Template APIs.
-
Support for Map/Reduce through the reactive Template API.
-
$jsonSchema
support for queries and collection creation. -
Change Stream support for imperative and reactive drivers.
-
Tailable cursors for imperative driver.
-
MongoDB 3.6 Session support for the imperative and reactive Template APIs.
-
MongoDB 4.0 Transaction support and a MongoDB-specific transaction manager implementation.
-
Default sort specifications for repository query methods using
@Query(sort=…)
. -
findAndReplace support through imperative and reactive Template APIs.
-
Deprecation of
dropDups
in@Indexed
and@CompoundIndex
as MongoDB server 3.0 and newer do not supportdropDups
anymore.
6.7. What’s New in Spring Data MongoDB 2.0
-
Upgrade to Java 8.
-
Usage of the
Document
API, instead ofDBObject
. -
Tailable Cursor queries.
-
Support for aggregation result streaming by using Java 8
Stream
. -
Fluent Collection API for CRUD and aggregation operations.
-
Kotlin extensions for Template and Collection APIs.
-
Integration of collations for collection and index creation and query operations.
-
Query-by-Example support without type matching.
-
Support for isolation
Update
operations. -
Tooling support for null-safety by using Spring’s
@NonNullApi
and@Nullable
annotations. -
Deprecated cross-store support and removed Log4j appender.
6.8. What’s New in Spring Data MongoDB 1.10
-
Compatible with MongoDB Server 3.4 and the MongoDB Java Driver 3.4.
-
New annotations for
@CountQuery
,@DeleteQuery
, and@ExistsQuery
. -
Extended support for MongoDB 3.2 and MongoDB 3.4 aggregation operators (see Supported Aggregation Operations).
-
Support for partial filter expression when creating indexes.
-
Publishing lifecycle events when loading or converting
DBRef
instances. -
Added any-match mode for Query By Example.
-
Support for
$caseSensitive
and$diacriticSensitive
text search. -
Support for GeoJSON Polygon with hole.
-
Performance improvements by bulk-fetching
DBRef
instances. -
Multi-faceted aggregations using
$facet
,$bucket
, and$bucketAuto
withAggregation
.
6.9. What’s New in Spring Data MongoDB 1.9
-
The following annotations have been enabled to build your own composed annotations:
@Document
,@Id
,@Field
,@Indexed
,@CompoundIndex
,@GeoSpatialIndexed
,@TextIndexed
,@Query
, and@Meta
. -
Support for Projections in repository query methods.
-
Support for Query by Example.
-
Out-of-the-box support for
java.util.Currency
in object mapping. -
Support for the bulk operations introduced in MongoDB 2.6.
-
Upgrade to Querydsl 4.
-
Assert compatibility with MongoDB 3.0 and MongoDB Java Driver 3.2.
6.10. What’s New in Spring Data MongoDB 1.8
-
Criteria
offers support for creating$geoIntersects
. -
Support for SpEL expressions in
@Query
. -
MongoMappingEvents
expose the collection name for which they are issued. -
Improved support for
<mongo:mongo-client credentials="…" />
. -
Improved index creation failure error message.
6.11. What’s New in Spring Data MongoDB 1.7
-
Assert compatibility with MongoDB 3.0 and MongoDB Java Driver 3-beta3.
-
Support JSR-310 and ThreeTen back-port date/time types.
-
Allow
Stream
as a query method return type (see: Query Methods). -
GeoJSON support in both domain types and queries (see: GeoJSON Support).
-
QueryDslPredicateExcecutor
now supportsfindAll(OrderSpecifier<?>… orders)
. -
Support calling JavaScript functions with Script Operations.
-
Improve support for
CONTAINS
keyword on collection-like properties. -
Support for
$bit
,$mul
, and$position
operators toUpdate
.
7. Upgrading from 2.x to 3.x
Spring Data MongoDB 3.x requires the MongoDB Java Driver 4.x.
The 4.0 MongoDB Java Driver does no longer support certain features that have already been deprecated in one of the last minor versions.
Some of the changes affect the initial setup configuration as well as compile/runtime features.
We summarized the most typical changes one might encounter.
Things to keep in mind when using the 4.0 driver:
-
IndexOperations.resetIndexCache()
is no longer supported. -
Any
MapReduceOptions.extraOption
is silently ignored. -
WriteResult
no longer holds error information but, instead, throws anException
. -
MongoOperations.executeInSession(…)
no longer callsrequestStart
andrequestDone
. -
Index name generation has become a driver-internal operation. Spring Data MongoDB still uses the 2.x schema to generate names.
-
Some
Exception
messages differ between the generation 2 and 3 servers as well as between the MMap.v1 and WiredTiger storage engines.
7.1. Dependency Changes
Instead of the single artifact uber-jar mongo-java-driver
, imports are now split to include separate artifacts:
-
org.mongodb:mongodb-driver-core
(required) -
org.mongodb:mongodb-driver-sync
(optional) -
org.mongodb:mongodb-driver-reactivestreams
(optional)
Depending on the application one of the mongodb-driver-sync
, mongodb-driver-reactivestreams
artifacts is is required next to the mandatory mongodb-driver-core
.
It is possible to combine the sync and reactive drivers in one application if needed.
7.2. Java Configuration
Type | Comment |
---|---|
|
Creates |
|
Uses |
|
Uses |
|
Uses |
|
Now produces |
|
Configuration methods use parameter injection instead of calling local methods to avoid the need for cglib proxies |
2.x | Replacement in 3.x | Comment |
---|---|---|
|
|
Creating a |
|
|
Using |
|
- |
- |
|
|
|
|
|
Returns |
|
7.3. XML Namespace
Element / Attribute | 2.x | 3.x |
---|---|---|
|
Used to create a |
Now exposes a |
|
Was a comma delimited list of replica set members (host/port) |
Now defines the replica set name. |
|
NONE, NORMAL, SAFE, FSYNC_SAFE, REPLICAS_SAFE, MAJORITY |
W1, W2, W3, UNAKNOWLEDGED, AKNOWLEDGED, JOURNALED, MAJORITY |
Element / Attribute | Replacement in 3.x | Comment |
---|---|---|
|
|
Referencing a |
|
|
Single authentication data instead of list. |
|
|
See |
Element | Comment |
---|---|
|
Replacement for |
|
Replacement for |
|
Replacement for |
|
Namespace element for |
2.x | Replacement in 3.x | Comment |
---|---|---|
|
|
|
|
|
|
|
|
7.4. Other Changes
7.4.1. Auto Index Creation
Annotation based index creation is now turned OFF by default and needs to be enabled eg. when relying on @GeoSpatialIndexed
.
Please refer to Index Creation on how to create indexes programmatically.
<mongo:mapping-converter auto-index-creation="true" /> (1)
@Configuration
public class Config extends AbstractMongoClientConfiguration {
@Override
protected boolean autoIndexCreation() { (2)
return true;
}
// ...
}
MongoDatabaseFactory dbFactory = new SimpleMongoClientDatabaseFactory(...);
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(dbFactory);
MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.setAutoIndexCreation(true); (3)
// ...
mappingContext.afterPropertiesSet();
MongoTemplate template = new MongoTemplate(dbFactory, new MappingMongoConverter(dbRefResolver, mappingContext));
1 | Use the XML namespace attribute auto-index-creation on mapping-converter . |
2 | Override autoIndexCreation via AbstractMongoClientConfiguration or AbstractReactiveMongoClientConfiguration . |
3 | Set the flag on MongoMappingContext . |
7.4.2. UUID Types
The MongoDB UUID representation can now be configured with different formats.
This has to be done via MongoClientSettings
as shown in the snippet below.
@Configuration
public class Config extends AbstractMongoClientConfiguration {
@Override
public void configureClientSettings(MongoClientSettings.Builder builder) {
builder.uuidRepresentation(UuidRepresentation.STANDARD);
}
// ...
}
7.4.3. Deferred MongoDatabase lookup in ReactiveMongoDatabaseFactory
ReactiveMongoDatabaseFactory
now returns Mono<MongoDatabase>
instead of MongoDatabase
to allow access to the Reactor Subscriber context to enable context-specific routing functionality.
This change affects ReactiveMongoTemplate.getMongoDatabase()
and ReactiveMongoTemplate.getCollection()
so both methods must follow deferred retrieval.
8. Dependencies
Due to the different inception dates of individual Spring Data modules, most of them carry different major and minor version numbers. The easiest way to find compatible ones is to rely on the Spring Data Release Train BOM that we ship with the compatible versions defined. In a Maven project, you would declare this dependency in the <dependencyManagement />
section of your POM as follows:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-bom</artifactId>
<version>2021.1.6</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
The current release train version is 2021.1.6
. The train version uses calver with the pattern YYYY.MINOR.MICRO
.
The version name follows ${calver}
for GA releases and service releases and the following pattern for all other versions: ${calver}-${modifier}
, where modifier
can be one of the following:
-
SNAPSHOT
: Current snapshots -
M1
,M2
, and so on: Milestones -
RC1
,RC2
, and so on: Release candidates
You can find a working example of using the BOMs in our Spring Data examples repository. With that in place, you can declare the Spring Data modules you would like to use without a version in the <dependencies />
block, as follows:
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependencies>
8.1. Dependency Management with Spring Boot
Spring Boot selects a recent version of Spring Data modules for you. If you still want to upgrade to a newer version, set
the spring-data-releasetrain.version
property to the train version and iteration you would like to use.
9. Working with Spring Data Repositories
The goal of the Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.
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. You should adapt the XML namespace declaration and the types to be extended to the equivalents of the particular module that you use. “Namespace reference” covers XML configuration, which is supported across all Spring Data modules that support the repository API. “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, see the chapter on that module of this document. |
9.1. Core concepts
The central interface in the Spring Data repository abstraction is Repository
.
It takes the domain class to manage as well as the ID type of the domain class as type arguments.
This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one.
The CrudRepository
interface provides sophisticated CRUD functionality for the entity class that is being managed.
CrudRepository
Interfacepublic interface CrudRepository<T, ID> extends Repository<T, ID> {
<S extends T> S save(S entity); (1)
Optional<T> findById(ID primaryKey); (2)
Iterable<T> findAll(); (3)
long count(); (4)
void delete(T entity); (5)
boolean existsById(ID primaryKey); (6)
// … more functionality omitted.
}
1 | Saves the given entity. |
2 | Returns the entity identified by the given ID. |
3 | Returns all entities. |
4 | Returns the number of entities. |
5 | Deletes the given entity. |
6 | Indicates whether an entity with the given ID exists. |
We also provide persistence technology-specific abstractions, such as 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 such as CrudRepository .
|
On top of the CrudRepository
, there is a PagingAndSortingRepository
abstraction that adds additional methods to ease paginated access to entities:
PagingAndSortingRepository
interfacepublic interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}
To access the second page of User
by a page size of 20, you could do something like the following:
PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(PageRequest.of(1, 20));
In addition to query methods, query derivation for both count and delete queries is available. The following list shows the interface definition for a derived count query:
interface UserRepository extends CrudRepository<User, Long> {
long countByLastname(String lastname);
}
The following listing shows the interface definition for a derived delete query:
interface UserRepository extends CrudRepository<User, Long> {
long deleteByLastname(String lastname);
List<User> removeByLastname(String lastname);
}
9.2. Query Methods
Standard CRUD functionality repositories usually have queries on the underlying datastore. With Spring Data, declaring those queries becomes a four-step process:
-
Declare an interface extending Repository or one of its subinterfaces and type it to the domain class and ID type that it should handle, as shown in the following example:
interface PersonRepository extends Repository<Person, Long> { … }
-
Declare query methods on the interface.
interface PersonRepository extends Repository<Person, Long> { List<Person> findByLastname(String lastname); }
-
Set up Spring to create proxy instances for those interfaces, either with JavaConfig or with XML configuration.
-
To use Java configuration, create a class similar to the following:
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @EnableJpaRepositories class Config { … }
-
To use XML configuration, define a bean similar to the following:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <jpa:repositories base-package="com.acme.repositories"/> </beans>
The JPA namespace is used in this example. If you use the repository abstraction for any other store, you need to change this to the appropriate namespace declaration of your store module. In other words, you should exchange
jpa
in favor of, for example,mongodb
.Also, note that the JavaConfig variant does not configure a package explicitly, because the package of the annotated class is used by default. To customize the package to scan, use one of the
basePackage…
attributes of the data-store-specific repository’s@Enable${store}Repositories
-annotation.
-
-
Inject the repository instance and use it, as shown in the following example:
class SomeClient { private final PersonRepository repository; SomeClient(PersonRepository repository) { this.repository = repository; } void doSomething() { List<Person> persons = repository.findByLastname("Matthews"); } }
The sections that follow explain each step in detail:
9.3. Defining Repository Interfaces
To define a repository interface, you first need to 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
.
9.3.1. Fine-tuning Repository Definition
Typically, your repository interface extends Repository
, CrudRepository
, or PagingAndSortingRepository
.
Alternatively, if you do not want to extend Spring Data interfaces, you can also annotate your repository interface with @RepositoryDefinition
.
Extending CrudRepository
exposes a complete set of methods to manipulate your entities.
If you prefer to be selective about the methods being exposed, copy the methods you want to expose from CrudRepository
into your domain repository.
Doing so lets you define your own abstractions on top of the provided Spring Data Repositories functionality. |
The following example shows how to selectively expose CRUD methods (findById
and save
, in this case):
@NoRepositoryBean
interface MyBaseRepository<T, ID> extends Repository<T, ID> {
Optional<T> findById(ID id);
<S extends T> S save(S entity);
}
interface UserRepository extends MyBaseRepository<User, Long> {
User findByEmailAddress(EmailAddress emailAddress);
}
In the prior example, you defined a common base interface for all your domain repositories and exposed findById(…)
as well as save(…)
.These methods are routed into the base repository implementation of the store of your choice provided by Spring Data (for example, if you use JPA, the implementation is SimpleJpaRepository
), because they match the method signatures in CrudRepository
.
So the UserRepository
can now save users, find individual users by ID, and trigger a query to find Users
by email address.
The intermediate repository interface is annotated with @NoRepositoryBean .
Make sure you add that annotation to all repository interfaces for which Spring Data should not create instances at runtime.
|
9.3.2. Using Repositories with Multiple Spring Data Modules
Using a unique Spring Data module in your application makes things simple, because all repository interfaces in the defined scope are bound to the Spring Data module. Sometimes, applications require using more than one Spring Data module. In such cases, a repository definition must distinguish between persistence technologies. When it detects multiple repository factories on the class path, Spring Data enters strict repository configuration mode. Strict configuration uses details on the repository or the domain class to decide about Spring Data module binding for a repository definition:
-
If the repository definition extends the module-specific repository, it is a valid candidate for the particular Spring Data module.
-
If the domain class is annotated with the module-specific type annotation, it is a valid candidate for the particular Spring Data module. Spring Data modules accept either third-party annotations (such as JPA’s
@Entity
) or provide their own annotations (such as@Document
for Spring Data MongoDB and Spring Data Elasticsearch).
The following example shows a repository that uses module-specific interfaces (JPA in this case):
interface MyRepository extends JpaRepository<User, Long> { }
@NoRepositoryBean
interface MyBaseRepository<T, ID> extends JpaRepository<T, ID> { … }
interface UserRepository extends MyBaseRepository<User, Long> { … }
MyRepository
and UserRepository
extend JpaRepository
in their type hierarchy.
They are valid candidates for the Spring Data JPA module.
The following example shows a repository that uses generic interfaces:
interface AmbiguousRepository extends Repository<User, Long> { … }
@NoRepositoryBean
interface MyBaseRepository<T, ID> extends CrudRepository<T, ID> { … }
interface AmbiguousUserRepository extends MyBaseRepository<User, Long> { … }
AmbiguousRepository
and AmbiguousUserRepository
extend only Repository
and CrudRepository
in their type hierarchy.
While this is fine when using a unique Spring Data module, multiple modules cannot distinguish to which particular Spring Data these repositories should be bound.
The following example shows a repository that uses domain classes with annotations:
interface PersonRepository extends Repository<Person, Long> { … }
@Entity
class Person { … }
interface UserRepository extends Repository<User, Long> { … }
@Document
class User { … }
PersonRepository
references Person
, which is annotated with the JPA @Entity
annotation, so this repository clearly belongs to Spring Data JPA. UserRepository
references User
, which is annotated with Spring Data MongoDB’s @Document
annotation.
The following bad example shows a repository that uses domain classes with mixed annotations:
interface JpaPersonRepository extends Repository<Person, Long> { … }
interface MongoDBPersonRepository extends Repository<Person, Long> { … }
@Entity
@Document
class Person { … }
This example shows a domain class using both JPA and Spring Data MongoDB annotations.
It defines two repositories, JpaPersonRepository
and MongoDBPersonRepository
.
One is intended for JPA and the other for MongoDB usage.
Spring Data is no longer able to tell the repositories apart, which leads to undefined behavior.
Repository type details and distinguishing domain class annotations are used for strict repository configuration to identify repository candidates for a particular Spring Data module. Using multiple persistence technology-specific annotations on the same domain type is possible and enables reuse of domain types across multiple persistence technologies. However, Spring Data can then no longer determine a unique module with which to bind the repository.
The last way to distinguish repositories is by scoping repository base packages. Base packages define the starting points for scanning for repository interface definitions, which implies having repository definitions located in the appropriate packages. By default, annotation-driven configuration uses the package of the configuration class. The base package in XML-based configuration is mandatory.
The following example shows annotation-driven configuration of base packages:
@EnableJpaRepositories(basePackages = "com.acme.repositories.jpa")
@EnableMongoRepositories(basePackages = "com.acme.repositories.mongo")
class Configuration { … }
9.4. Defining Query Methods
The repository proxy has two ways to derive a store-specific query from the method name:
-
By deriving the query from the method name directly.
-
By using a manually defined query.
Available options depend on the actual store. However, there must be a strategy that decides what actual query is created. The next section describes the available options.
9.4.1. Query Lookup Strategies
The following strategies are available for the repository infrastructure to resolve the query.
With XML configuration, you can configure the strategy at the namespace through the query-lookup-strategy
attribute.
For Java configuration, you can use the queryLookupStrategy
attribute of the Enable${store}Repositories
annotation.
Some strategies may not be supported for particular datastores.
-
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. You can read more about query construction in “Query Creation”. -
USE_DECLARED_QUERY
tries to find a declared query and throws an exception if it cannot find one. The query can be defined by an annotation somewhere or declared by other means. See 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
(the default) combinesCREATE
andUSE_DECLARED_QUERY
. It looks up a declared query first, and, if no declared query is found, it creates a custom method name-based query. This is the default lookup strategy and, thus, is used if you do not configure anything explicitly. It allows quick query definition by method names but also custom-tuning of these queries by introducing declared queries as needed.
9.4.2. Query Creation
The query builder mechanism built into the Spring Data repository infrastructure is useful for building constraining queries over entities of the repository.
The following example shows how to create a number of queries:
interface PersonRepository extends Repository<Person, Long> {
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
// Enables the distinct flag for the query
List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
// Enabling ignoring case for an individual property
List<Person> findByLastnameIgnoreCase(String lastname);
// Enabling ignoring case for all suitable properties
List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
// Enabling static ORDER BY for a query
List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
}
Parsing query method names is divided into subject and predicate.
The first part (find…By
, exists…By
) defines the subject of the query, the second part forms the predicate.
The introducing clause (subject) can contain further expressions.
Any text between find
(or other introducing keywords) and By
is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct
to set a distinct flag on the query to be created or Top
/First
to limit query results.
The appendix contains the full list of query method subject keywords and query method predicate keywords including sorting and letter-casing modifiers.
However, the first By
acts as a delimiter to indicate the start of the actual criteria predicate.
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
andOR
. You also get support for operators such asBetween
,LessThan
,GreaterThan
, andLike
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 supports ignoring case (usuallyString
instances — 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
orDesc
). To create a query method that supports dynamic sorting, see “Special parameter handling”.
9.4.3. 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. Consider the following method signature:
List<Person> findByAddressZipCode(ZipCode zipCode);
Assume a Person
has an Address
with a ZipCode
.
In that case, the method creates the x.address.zipCode
property traversal.
The resolution algorithm starts by 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 continues building the tree down from there, splitting the tail up in the way just described.
If the first split does not match, the algorithm moves 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, choose the wrong property, and 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 be as follows:
List<Person> findByAddress_ZipCode(ZipCode zipCode);
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
9.4.4. Special parameter handling
To handle parameters in your query, define method parameters as already seen in the preceding examples.
Besides that, the infrastructure recognizes certain specific types like Pageable
and Sort
, to apply pagination and sorting to your queries dynamically.
The following example demonstrates these features:
Pageable
, Slice
, and Sort
in query methodsPage<User> findByLastname(String lastname, Pageable pageable);
Slice<User> findByLastname(String lastname, Pageable pageable);
List<User> findByLastname(String lastname, Sort sort);
List<User> findByLastname(String lastname, Pageable pageable);
APIs taking Sort and Pageable expect non-null values to be handed into methods.
If you do not want to apply any sorting or pagination, use Sort.unsorted() and Pageable.unpaged() .
|
The first method lets you pass an org.springframework.data.domain.Pageable
instance to the query method to dynamically add paging to your statically defined query.
A Page
knows about the total number of elements and pages available.
It does so by the infrastructure triggering a count query to calculate the overall number.
As this might be expensive (depending on the store used), you can instead return a Slice
.
A Slice
knows only about whether a next Slice
is available, which might be sufficient when walking through a larger result set.
Sorting options are handled through the Pageable
instance, too.
If you need only sorting, add an org.springframework.data.domain.Sort
parameter to your method.
As you can see, returning a List
is also possible.
In this case, the additional metadata required to build the actual Page
instance is not created (which, in turn, means that the additional count query that would have been necessary is not issued).
Rather, it restricts the query to look up only the given range of entities.
To find out how many pages you get for an entire query, you have to trigger an additional count query. By default, this query is derived from the query you actually trigger. |
Paging and Sorting
You can define simple sorting expressions by using property names. You can concatenate expressions to collect multiple criteria into one expression.
Sort sort = Sort.by("firstname").ascending()
.and(Sort.by("lastname").descending());
For a more type-safe way to define sort expressions, start with the type for which to define the sort expression and use method references to define the properties on which to sort.
TypedSort<Person> person = Sort.sort(Person.class);
Sort sort = person.by(Person::getFirstname).ascending()
.and(person.by(Person::getLastname).descending());
TypedSort.by(…) makes use of runtime proxies by (typically) using CGlib, which may interfere with native image compilation when using tools such as Graal VM Native.
|
If your store implementation supports Querydsl, you can also use the generated metamodel types to define sort expressions:
QSort sort = QSort.by(QPerson.firstname.asc())
.and(QSort.by(QPerson.lastname.desc()));
9.4.5. Limiting Query Results
You can limit the results of query methods by using the first
or top
keywords, which you can use interchangeably.
You can append an optional numeric value to top
or first
to specify the maximum result size to be returned.
If the number is left out, a result size of 1 is assumed.
The following example shows how to limit the query size:
Top
and First
User findFirstByOrderByLastnameAsc();
User findTopByOrderByAgeDesc();
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
List<User> findFirst10ByLastname(String lastname, Sort sort);
List<User> findTop10ByLastname(String lastname, Pageable pageable);
The limiting expressions also support the Distinct
keyword for datastores that support distinct queries.
Also, for the queries that limit the result set to one instance, wrapping the result into with the Optional
keyword is supported.
If pagination or slicing is applied to a limiting query pagination (and the calculation of the number of available pages), it is applied within the limited result.
Limiting the results in combination with dynamic sorting by using a Sort parameter lets you express query methods for the 'K' smallest as well as for the 'K' biggest elements.
|
9.4.6. Repository Methods Returning Collections or Iterables
Query methods that return multiple results can use standard Java Iterable
, List
, and Set
.
Beyond that, we support returning Spring Data’s Streamable
, a custom extension of Iterable
, as well as collection types provided by Vavr.
Refer to the appendix explaining all possible query method return types.
Using Streamable as Query Method Return Type
You can use Streamable
as alternative to Iterable
or any collection type.
It provides convenience methods to access a non-parallel Stream
(missing from Iterable
) and the ability to directly ….filter(…)
and ….map(…)
over the elements and concatenate the Streamable
to others:
interface PersonRepository extends Repository<Person, Long> {
Streamable<Person> findByFirstnameContaining(String firstname);
Streamable<Person> findByLastnameContaining(String lastname);
}
Streamable<Person> result = repository.findByFirstnameContaining("av")
.and(repository.findByLastnameContaining("ea"));
Returning Custom Streamable Wrapper Types
Providing dedicated wrapper types for collections is a commonly used pattern to provide an API for a query result that returns multiple elements. Usually, these types are used by invoking a repository method returning a collection-like type and creating an instance of the wrapper type manually. You can avoid that additional step as Spring Data lets you use these wrapper types as query method return types if they meet the following criteria:
-
The type implements
Streamable
. -
The type exposes either a constructor or a static factory method named
of(…)
orvalueOf(…)
that takesStreamable
as an argument.
The following listing shows an example:
class Product { (1)
MonetaryAmount getPrice() { … }
}
@RequiredArgsConstructor(staticName = "of")
class Products implements Streamable<Product> { (2)
private final Streamable<Product> streamable;
public MonetaryAmount getTotal() { (3)
return streamable.stream()
.map(Priced::getPrice)
.reduce(Money.of(0), MonetaryAmount::add);
}
@Override
public Iterator<Product> iterator() { (4)
return streamable.iterator();
}
}
interface ProductRepository implements Repository<Product, Long> {
Products findAllByDescriptionContaining(String text); (5)
}
1 | A Product entity that exposes API to access the product’s price. |
2 | A wrapper type for a Streamable<Product> that can be constructed by using Products.of(…) (factory method created with the Lombok annotation).
A standard constructor taking the Streamable<Product> will do as well. |
3 | The wrapper type exposes an additional API, calculating new values on the Streamable<Product> . |
4 | Implement the Streamable interface and delegate to the actual result. |
5 | That wrapper type Products can be used directly as a query method return type.
You do not need to return Streamable<Product> and manually wrap it after the query in the repository client. |
Support for Vavr Collections
Vavr is a library that embraces functional programming concepts in Java. It ships with a custom set of collection types that you can use as query method return types, as the following table shows:
Vavr collection type | Used Vavr implementation type | Valid Java source types |
---|---|---|
|
|
|
|
|
|
|
|
|
You can use the types in the first column (or subtypes thereof) as query method return types and get the types in the second column used as implementation type, depending on the Java type of the actual query result (third column).
Alternatively, you can declare Traversable
(the Vavr Iterable
equivalent), and we then derive the implementation class from the actual return value.
That is, a java.util.List
is turned into a Vavr List
or Seq
, a java.util.Set
becomes a Vavr LinkedHashSet
Set
, and so on.
9.4.7. Null Handling of Repository Methods
As of Spring Data 2.0, repository CRUD methods that return an individual aggregate instance use Java 8’s Optional
to indicate the potential absence of a value.
Besides that, Spring Data supports returning the following wrapper types on query methods:
-
com.google.common.base.Optional
-
scala.Option
-
io.vavr.control.Option
Alternatively, query methods can choose not to use a wrapper type at all.
The absence of a query result is then indicated by returning null
.
Repository methods returning collections, collection alternatives, wrappers, and streams are guaranteed never to return null
but rather the corresponding empty representation.
See “Repository query return types” for details.
Nullability Annotations
You can express nullability constraints for repository methods by using Spring Framework’s nullability annotations.
They provide a tooling-friendly approach and opt-in null
checks during runtime, as follows:
-
@NonNullApi
: Used on the package level to declare that the default behavior for parameters and return values is, respectively, neither to accept nor to producenull
values. -
@NonNull
: Used on a parameter or return value that must not benull
(not needed on a parameter and return value where@NonNullApi
applies). -
@Nullable
: Used on a parameter or return value that can benull
.
Spring annotations are meta-annotated with JSR 305 annotations (a dormant but widely used JSR).
JSR 305 meta-annotations let tooling vendors (such as IDEA, Eclipse, and Kotlin) provide null-safety support in a generic way, without having to hard-code support for Spring annotations.
To enable runtime checking of nullability constraints for query methods, you need to activate non-nullability on the package level by using Spring’s @NonNullApi
in package-info.java
, as shown in the following example:
package-info.java
@org.springframework.lang.NonNullApi
package com.acme;
Once non-null defaulting is in place, repository query method invocations get validated at runtime for nullability constraints.
If a query result violates the defined constraint, an exception is thrown.
This happens when the method would return null
but is declared as non-nullable (the default with the annotation defined on the package in which the repository resides).
If you want to opt-in to nullable results again, selectively use @Nullable
on individual methods.
Using the result wrapper types mentioned at the start of this section continues to work as expected: an empty result is translated into the value that represents absence.
The following example shows a number of the techniques just described:
package com.acme; (1)
import org.springframework.lang.Nullable;
interface UserRepository extends Repository<User, Long> {
User getByEmailAddress(EmailAddress emailAddress); (2)
@Nullable
User findByEmailAddress(@Nullable EmailAddress emailAdress); (3)
Optional<User> findOptionalByEmailAddress(EmailAddress emailAddress); (4)
}
1 | The repository resides in a package (or sub-package) for which we have defined non-null behavior. |
2 | Throws an EmptyResultDataAccessException when the query does not produce a result.
Throws an IllegalArgumentException when the emailAddress handed to the method is null . |
3 | Returns null when the query does not produce a result.
Also accepts null as the value for emailAddress . |
4 | Returns Optional.empty() when the query does not produce a result.
Throws an IllegalArgumentException when the emailAddress handed to the method is null . |
Nullability in Kotlin-based Repositories
Kotlin has the definition of nullability constraints baked into the language.
Kotlin code compiles to bytecode, which does not express nullability constraints through method signatures but rather through compiled-in metadata.
Make sure to include the kotlin-reflect
JAR in your project to enable introspection of Kotlin’s nullability constraints.
Spring Data repositories use the language mechanism to define those constraints to apply the same runtime checks, as follows:
interface UserRepository : Repository<User, String> {
fun findByUsername(username: String): User (1)
fun findByFirstname(firstname: String?): User? (2)
}
1 | The method defines both the parameter and the result as non-nullable (the Kotlin default).
The Kotlin compiler rejects method invocations that pass null to the method.
If the query yields an empty result, an EmptyResultDataAccessException is thrown. |
2 | This method accepts null for the firstname parameter and returns null if the query does not produce a result. |
9.4.8. Streaming Query Results
You can process the results of query methods incrementally by using a Java 8 Stream<T>
as the return type.
Instead of wrapping the query results in a Stream
, data store-specific methods are used to perform the streaming, as shown in the following example:
Stream<T>
@Query("select u from User u")
Stream<User> findAllByCustomQueryAndStream();
Stream<User> readAllByFirstnameNotNull();
@Query("select u from User u")
Stream<User> streamAllPaged(Pageable pageable);
A Stream potentially wraps underlying data store-specific resources and must, therefore, be closed after usage.
You can either manually close the Stream by using the close() method or by using a Java 7 try-with-resources block, as shown in the following example:
|
Stream<T>
result in a try-with-resources
blocktry (Stream<User> stream = repository.findAllByCustomQueryAndStream()) {
stream.forEach(…);
}
Not all Spring Data modules currently support Stream<T> as a return type.
|
9.4.9. Asynchronous Query Results
You can run repository queries asynchronously by using Spring’s asynchronous method running capability.
This means the method returns immediately upon invocation while the actual query occurs in a task that has been submitted to a Spring TaskExecutor
.
Asynchronous queries differ from reactive queries and should not be mixed.
See the store-specific documentation for more details on reactive support.
The following example shows a number of asynchronous queries:
@Async
Future<User> findByFirstname(String firstname); (1)
@Async
CompletableFuture<User> findOneByFirstname(String firstname); (2)
@Async
ListenableFuture<User> findOneByLastname(String lastname); (3)
1 | Use java.util.concurrent.Future as the return type. |
2 | Use a Java 8 java.util.concurrent.CompletableFuture as the return type. |
3 | Use a org.springframework.util.concurrent.ListenableFuture as the return type. |
9.5. Creating Repository Instances
This section covers how to create instances and bean definitions for the defined repository interfaces. One way to do so is by using the Spring namespace that is shipped with each Spring Data module that supports the repository mechanism, although we generally recommend using Java configuration.
9.5.1. XML Configuration
Each Spring Data module includes a repositories
element that lets you define a base package that Spring scans for you, as shown in the following example:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<repositories base-package="com.acme.repositories" />
</beans:beans>
In the preceding example, Spring is instructed to scan com.acme.repositories
and all its sub-packages for interfaces extending Repository
or one of its sub-interfaces.
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
.
Bean names for nested repository interfaces are prefixed with their enclosing type name.
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 that extends the persistence technology-specific Repository
sub-interface located under the configured base package and creates a bean instance for it.
However, you might want more fine-grained control over which interfaces have bean instances created for them.
To do so, use <include-filter />
and <exclude-filter />
elements inside the <repositories />
element.
The semantics are exactly equivalent to the elements in Spring’s context namespace.
For details, see the Spring reference documentation for these elements.
For example, to exclude certain interfaces from instantiation as repository beans, you could use the following configuration:
<repositories base-package="com.acme.repositories">
<context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>
The preceding example excludes all interfaces ending in SomeRepository
from being instantiated.
9.5.2. Java Configuration
You can also trigger the repository infrastructure by using a store-specific @Enable${store}Repositories
annotation on a Java configuration class. For an introduction to Java-based configuration of the Spring container, see JavaConfig in the Spring reference documentation.
A sample configuration to enable Spring Data repositories resembles the following:
@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {
@Bean
EntityManagerFactory entityManagerFactory() {
// …
}
}
The preceding example 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. See the sections covering the store-specific configuration.
|
9.5.3. Standalone Usage
You can also use the repository infrastructure outside of a Spring container — for example, 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 with a persistence technology-specific RepositoryFactory
that you can use, as follows:
RepositoryFactorySupport factory = … // Instantiate factory here
UserRepository repository = factory.getRepository(UserRepository.class);
9.6. Custom Implementations for Spring Data Repositories
Spring Data provides various options to create query methods with little coding. But when those options don’t fit your needs you can also provide your own custom implementation for repository methods. This section describes how to do that.
9.6.1. Customizing Individual Repositories
To enrich a repository with custom functionality, you must first define a fragment interface and an implementation for the custom functionality, as follows:
interface CustomizedUserRepository {
void someCustomMethod(User user);
}
class CustomizedUserRepositoryImpl implements CustomizedUserRepository {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
The most important part of the class name that corresponds to the fragment interface is the Impl postfix.
|
The implementation itself does not depend on Spring Data and can be a regular Spring bean.
Consequently, you can use standard dependency injection behavior to inject references to other beans (such as a JdbcTemplate
), take part in aspects, and so on.
Then you can let your repository interface extend the fragment interface, as follows:
interface UserRepository extends CrudRepository<User, Long>, CustomizedUserRepository {
// Declare query methods here
}
Extending the fragment interface with your repository interface combines the CRUD and custom functionality and makes it available to clients.
Spring Data repositories are implemented by using fragments that form a repository composition. Fragments are the base repository, functional aspects (such as QueryDsl), and custom interfaces along with their implementations. Each time you add an interface to your repository interface, you enhance the composition by adding a fragment. The base repository and repository aspect implementations are provided by each Spring Data module.
The following example shows custom interfaces and their implementations:
interface HumanRepository {
void someHumanMethod(User user);
}
class HumanRepositoryImpl implements HumanRepository {
public void someHumanMethod(User user) {
// Your custom implementation
}
}
interface ContactRepository {
void someContactMethod(User user);
User anotherContactMethod(User user);
}
class ContactRepositoryImpl implements ContactRepository {
public void someContactMethod(User user) {
// Your custom implementation
}
public User anotherContactMethod(User user) {
// Your custom implementation
}
}
The following example shows the interface for a custom repository that extends CrudRepository
:
interface UserRepository extends CrudRepository<User, Long>, HumanRepository, ContactRepository {
// Declare query methods here
}
Repositories may be composed of multiple custom implementations that are imported in the order of their declaration. Custom implementations have a higher priority than the base implementation and repository aspects. This ordering lets you override base repository and aspect methods and resolves ambiguity if two fragments contribute the same method signature. Repository fragments are not limited to use in a single repository interface. Multiple repositories may use a fragment interface, letting you reuse customizations across different repositories.
The following example shows a repository fragment and its implementation:
save(…)
interface CustomizedSave<T> {
<S extends T> S save(S entity);
}
class CustomizedSaveImpl<T> implements CustomizedSave<T> {
public <S extends T> S save(S entity) {
// Your custom implementation
}
}
The following example shows a repository that uses the preceding repository fragment:
interface UserRepository extends CrudRepository<User, Long>, CustomizedSave<User> {
}
interface PersonRepository extends CrudRepository<Person, Long>, CustomizedSave<Person> {
}
Configuration
If you use namespace configuration, the repository infrastructure tries to autodetect custom implementation fragments by scanning for classes below the package in which it found a repository.
These classes need to follow the naming convention of appending the namespace element’s repository-impl-postfix
attribute to the fragment interface name.
This postfix defaults to Impl
.
The following example shows a repository that uses the default postfix and a repository that sets a custom value for the postfix:
<repositories base-package="com.acme.repository" />
<repositories base-package="com.acme.repository" repository-impl-postfix="MyPostfix" />
The first configuration in the preceding example tries to look up a class called com.acme.repository.CustomizedUserRepositoryImpl
to act as a custom repository implementation.
The second example tries to look up com.acme.repository.CustomizedUserRepositoryMyPostfix
.
Resolution of Ambiguity
If multiple implementations with matching class names are found in different packages, Spring Data uses the bean names to identify which one to use.
Given the following two custom implementations for the CustomizedUserRepository
shown earlier, the first implementation is used.
Its bean name is customizedUserRepositoryImpl
, which matches that of the fragment interface (CustomizedUserRepository
) plus the postfix Impl
.
package com.acme.impl.one;
class CustomizedUserRepositoryImpl implements CustomizedUserRepository {
// Your custom implementation
}
package com.acme.impl.two;
@Component("specialCustomImpl")
class CustomizedUserRepositoryImpl implements CustomizedUserRepository {
// Your custom implementation
}
If you annotate the UserRepository
interface with @Component("specialCustom")
, the bean name plus Impl
then matches the one defined for the repository implementation in com.acme.impl.two
, and it is used instead of the first one.
Manual Wiring
If your custom implementation uses annotation-based configuration and autowiring only, the preceding approach shown works well, because it is treated as any other Spring bean. If your implementation fragment bean needs special wiring, you can declare the bean and name it according to the conventions described in the preceding section. The infrastructure then refers to the manually defined bean definition by name instead of creating one itself. The following example shows how to manually wire a custom implementation:
<repositories base-package="com.acme.repository" />
<beans:bean id="userRepositoryImpl" class="…">
<!-- further configuration -->
</beans:bean>
9.6.2. Customize the Base Repository
The approach described in the preceding section requires customization of each repository interfaces when you want to customize the base repository behavior so that all repositories are affected. To instead change behavior for all repositories, you can create an implementation that extends the persistence technology-specific repository base class. This class then acts as a custom base class for the repository proxies, as shown in the following example:
class MyRepositoryImpl<T, ID>
extends SimpleJpaRepository<T, ID> {
private final EntityManager entityManager;
MyRepositoryImpl(JpaEntityInformation entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
// Keep the EntityManager around to used from the newly introduced methods.
this.entityManager = entityManager;
}
@Transactional
public <S extends T> S save(S entity) {
// implementation goes here
}
}
The class needs to have a constructor of the super class which the store-specific repository factory implementation uses.
If the repository base class has multiple constructors, override the one taking an EntityInformation plus a store specific infrastructure object (such as an EntityManager or a template class).
|
The final step is to make the Spring Data infrastructure aware of the customized repository base class.
In Java configuration, you can do so by using the repositoryBaseClass
attribute of the @Enable${store}Repositories
annotation, as shown in the following example:
@Configuration
@EnableJpaRepositories(repositoryBaseClass = MyRepositoryImpl.class)
class ApplicationConfiguration { … }
A corresponding attribute is available in the XML namespace, as shown in the following example:
<repositories base-package="com.acme.repository"
base-class="….MyRepositoryImpl" />
9.7. Publishing Events from Aggregate Roots
Entities managed by repositories are aggregate roots.
In a Domain-Driven Design application, these aggregate roots usually publish domain events.
Spring Data provides an annotation called @DomainEvents
that you can use on a method of your aggregate root to make that publication as easy as possible, as shown in the following example:
class AnAggregateRoot {
@DomainEvents (1)
Collection<Object> domainEvents() {
// … return events you want to get published here
}
@AfterDomainEventPublication (2)
void callbackMethod() {
// … potentially clean up domain events list
}
}
1 | The method that uses @DomainEvents can return either a single event instance or a collection of events.
It must not take any arguments. |
2 | After all events have been published, we have a method annotated with @AfterDomainEventPublication .
You can use it to potentially clean the list of events to be published (among other uses). |
The methods are called every time one of a Spring Data repository’s save(…)
, saveAll(…)
, delete(…)
or deleteAll(…)
methods are called.
9.8. Spring Data Extensions
This section documents a set of Spring Data extensions that enable Spring Data usage in a variety of contexts. Currently, most of the integration is targeted towards Spring MVC.
9.8.1. Querydsl Extension
Querydsl is a framework that enables the construction of statically typed SQL-like queries through its fluent API.
Several Spring Data modules offer integration with Querydsl through QuerydslPredicateExecutor
, as the following example shows:
public interface QuerydslPredicateExecutor<T> {
Optional<T> findById(Predicate predicate); (1)
Iterable<T> findAll(Predicate predicate); (2)
long count(Predicate predicate); (3)
boolean exists(Predicate predicate); (4)
// … more functionality omitted.
}
1 | Finds and returns a single entity matching the Predicate . |
2 | Finds and returns all entities matching the Predicate . |
3 | Returns the number of entities matching the Predicate . |
4 | Returns whether an entity that matches the Predicate exists. |
To use the Querydsl support, extend QuerydslPredicateExecutor
on your repository interface, as the following example shows:
interface UserRepository extends CrudRepository<User, Long>, QuerydslPredicateExecutor<User> {
}
The preceding example lets you write type-safe queries by using Querydsl Predicate
instances, as the following example shows:
Predicate predicate = user.firstname.equalsIgnoreCase("dave")
.and(user.lastname.startsWithIgnoreCase("mathews"));
userRepository.findAll(predicate);
9.8.2. Web support
Spring Data modules that support the repository programming model ship with a variety of web support.
The web related components require Spring MVC JARs to be on the classpath.
Some of them even provide integration with Spring HATEOAS.
In general, the integration support is enabled by using the @EnableSpringDataWebSupport
annotation in your JavaConfig configuration class, as the following example shows:
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
class WebConfiguration {}
The @EnableSpringDataWebSupport
annotation registers a few components.
We discuss those later in this section.
It also detects Spring HATEOAS on the classpath and registers integration components (if present) for it as well.
Alternatively, if you use XML configuration, register either SpringDataWebConfiguration
or HateoasAwareSpringDataWebConfiguration
as Spring beans, as the following example shows (for SpringDataWebConfiguration
):
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />
<!-- If you use Spring HATEOAS, register this one *instead* of the former -->
<bean class="org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration" />
Basic Web Support
The configuration shown in the previous section registers a few basic components:
-
A Using the
DomainClassConverter
Class to let Spring MVC resolve instances of repository-managed domain classes from request parameters or path variables. -
HandlerMethodArgumentResolver
implementations to let Spring MVC resolvePageable
andSort
instances from request parameters. -
Jackson Modules to de-/serialize types like
Point
andDistance
, or store specific ones, depending on the Spring Data Module used.
Using the DomainClassConverter
Class
The DomainClassConverter
class lets you use domain types in your Spring MVC controller method signatures directly so that you need not manually lookup the instances through the repository, as the following example shows:
@Controller
@RequestMapping("/users")
class UserController {
@RequestMapping("/{id}")
String showUserForm(@PathVariable("id") User user, Model model) {
model.addAttribute("user", user);
return "userForm";
}
}
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 findById(…)
on the repository instance registered for the domain type.
Currently, the repository has to implement CrudRepository to be eligible to be discovered for conversion.
|
HandlerMethodArgumentResolvers for Pageable and Sort
The configuration snippet shown in the previous section also registers a PageableHandlerMethodArgumentResolver
as well as an instance of SortHandlerMethodArgumentResolver
.
The registration enables Pageable
and Sort
as valid controller method arguments, as the following example shows:
@Controller
@RequestMapping("/users")
class UserController {
private final UserRepository repository;
UserController(UserRepository repository) {
this.repository = repository;
}
@RequestMapping
String showUsers(Model model, Pageable pageable) {
model.addAttribute("users", repository.findAll(pageable));
return "users";
}
}
The preceding method signature causes Spring MVC try to derive a Pageable
instance from the request parameters by using the following default configuration:
|
Page you want to retrieve. 0-indexed and defaults to 0. |
|
Size of the page you want to retrieve. Defaults to 20. |
|
Properties that should be sorted by in the format |
To customize this behavior, register a bean that implements the PageableHandlerMethodArgumentResolverCustomizer
interface or the SortHandlerMethodArgumentResolverCustomizer
interface, respectively.
Its customize()
method gets called, letting you change settings, as the following example shows:
@Bean SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
return s -> s.setPropertyDelimiter("<-->");
}
If setting the properties of an existing MethodArgumentResolver
is not sufficient for your purpose, extend either SpringDataWebConfiguration
or the HATEOAS-enabled equivalent, override the pageableResolver()
or sortResolver()
methods, and import your customized configuration file instead of using the @Enable
annotation.
If you need multiple Pageable
or Sort
instances 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}_
.
The following example shows the resulting method signature:
String showUsers(Model model,
@Qualifier("thing1") Pageable first,
@Qualifier("thing2") Pageable second) { … }
You have to populate thing1_page
, thing2_page
, and so on.
The default Pageable
passed into the method is equivalent to a PageRequest.of(0, 20)
, but you can customize it by using the @PageableDefault
annotation on the Pageable
parameter.
Hypermedia Support for Pageables
Spring HATEOAS ships with a representation model class (PagedResources
) that allows enriching 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, called the PagedResourcesAssembler
.
The following example shows how to use a PagedResourcesAssembler
as a controller method argument:
@Controller
class PersonController {
@Autowired PersonRepository repository;
@RequestMapping(value = "/persons", method = RequestMethod.GET)
HttpEntity<PagedResources<Person>> persons(Pageable pageable,
PagedResourcesAssembler assembler) {
Page<Person> persons = repository.findAll(pageable);
return new ResponseEntity<>(assembler.toResources(persons), HttpStatus.OK);
}
}
Enabling the configuration, as shown in the preceding example, lets the PagedResourcesAssembler
be used as a controller method argument.
Calling toResources(…)
on it has the following effects:
-
The content of the
Page
becomes the content of thePagedResources
instance. -
The
PagedResources
object gets aPageMetadata
instance attached, and it is populated with information from thePage
and the underlyingPageRequest
. -
The
PagedResources
may getprev
andnext
links attached, depending on the page’s state. The links point to the URI to which the method maps. The pagination parameters added to the method match the setup of thePageableHandlerMethodArgumentResolver
to make sure the links can be resolved later.
Assume we have 30 Person
instances in the database.
You can now trigger a request (GET http://localhost:8080/persons
) and see output similar to the following:
{ "links" : [ { "rel" : "next",
"href" : "http://localhost:8080/persons?page=1&size=20" }
],
"content" : [
… // 20 Person instances rendered here
],
"pageMetadata" : {
"size" : 20,
"totalElements" : 30,
"totalPages" : 2,
"number" : 0
}
}
The assembler produced the correct URI and also picked up the default configuration to resolve the parameters into a Pageable
for an upcoming request.
This means that, if you change that configuration, the links automatically adhere to the change.
By default, the assembler points to the controller method it was invoked in, but you can customize that by passing a custom Link
to be used as base to build the pagination links, which overloads the PagedResourcesAssembler.toResource(…)
method.
Spring Data Jackson Modules
The core module, and some of the store specific ones, ship with a set of Jackson Modules for types, like org.springframework.data.geo.Distance
and org.springframework.data.geo.Point
, used by the Spring Data domain.
Those Modules are imported once web support is enabled and com.fasterxml.jackson.databind.ObjectMapper
is available.
During initialization SpringDataJacksonModules
, like the SpringDataJacksonConfiguration
, get picked up by the infrastructure, so that the declared com.fasterxml.jackson.databind.Module
s are made available to the Jackson ObjectMapper
.
Data binding mixins for the following domain types are registered by the common infrastructure.
org.springframework.data.geo.Distance org.springframework.data.geo.Point org.springframework.data.geo.Box org.springframework.data.geo.Circle org.springframework.data.geo.Polygon
The individual module may provide additional |
Web Databinding Support
You can use Spring Data projections (described in Projections) to bind incoming request payloads by using either JSONPath expressions (requires Jayway JsonPath) or XPath expressions (requires XmlBeam), as the following example shows:
@ProjectedPayload
public interface UserPayload {
@XBRead("//firstname")
@JsonPath("$..firstname")
String getFirstname();
@XBRead("/lastname")
@JsonPath({ "$.lastname", "$.user.lastname" })
String getLastname();
}
You can use the type shown in the preceding example as a Spring MVC handler method argument or by using ParameterizedTypeReference
on one of methods of the RestTemplate
.
The preceding method declarations would try to find firstname
anywhere in the given document.
The lastname
XML lookup is performed on the top-level of the incoming document.
The JSON variant of that tries a top-level lastname
first but also tries lastname
nested in a user
sub-document if the former does not return a value.
That way, changes in the structure of the source document can be mitigated easily without having clients calling the exposed methods (usually a drawback of class-based payload binding).
Nested projections are supported as described in Projections.
If the method returns a complex, non-interface type, a Jackson ObjectMapper
is used to map the final value.
For Spring MVC, the necessary converters are registered automatically as soon as @EnableSpringDataWebSupport
is active and the required dependencies are available on the classpath.
For usage with RestTemplate
, register a ProjectingJackson2HttpMessageConverter
(JSON) or XmlBeamHttpMessageConverter
manually.
For more information, see the web projection example in the canonical Spring Data Examples repository.
Querydsl Web Support
For those stores that have QueryDSL integration, you can derive queries from the attributes contained in a Request
query string.
Consider the following query string:
?firstname=Dave&lastname=Matthews
Given the User
object from the previous examples, you can resolve a query string to the following value by using the QuerydslPredicateArgumentResolver
, as follows:
QUser.user.firstname.eq("Dave").and(QUser.user.lastname.eq("Matthews"))
The feature is automatically enabled, along with @EnableSpringDataWebSupport , when Querydsl is found on the classpath.
|
Adding a @QuerydslPredicate
to the method signature provides a ready-to-use Predicate
, which you can run by using the QuerydslPredicateExecutor
.
Type information is typically resolved from the method’s return type.
Since that information does not necessarily match the domain type, it might be a good idea to use the root attribute of QuerydslPredicate .
|
The following example shows how to use @QuerydslPredicate
in a method signature:
@Controller
class UserController {
@Autowired UserRepository repository;
@RequestMapping(value = "/", method = RequestMethod.GET)
String index(Model model, @QuerydslPredicate(root = User.class) Predicate predicate, (1)
Pageable pageable, @RequestParam MultiValueMap<String, String> parameters) {
model.addAttribute("users", repository.findAll(predicate, pageable));
return "index";
}
}
1 | Resolve query string arguments to matching Predicate for User . |
The default binding is as follows:
-
Object
on simple properties aseq
. -
Object
on collection like properties ascontains
. -
Collection
on simple properties asin
.
You can customize those bindings through the bindings
attribute of @QuerydslPredicate
or by making use of Java 8 default methods
and adding the QuerydslBinderCustomizer
method to the repository interface, as follows:
interface UserRepository extends CrudRepository<User, String>,
QuerydslPredicateExecutor<User>, (1)
QuerydslBinderCustomizer<QUser> { (2)
@Override
default void customize(QuerydslBindings bindings, QUser user) {
bindings.bind(user.username).first((path, value) -> path.contains(value)) (3)
bindings.bind(String.class)
.first((StringPath path, String value) -> path.containsIgnoreCase(value)); (4)
bindings.excluding(user.password); (5)
}
}
1 | QuerydslPredicateExecutor provides access to specific finder methods for Predicate . |
2 | QuerydslBinderCustomizer defined on the repository interface is automatically picked up and shortcuts @QuerydslPredicate(bindings=…) . |
3 | Define the binding for the username property to be a simple contains binding. |
4 | Define the default binding for String properties to be a case-insensitive contains match. |
5 | Exclude the password property from Predicate resolution. |
You can register a QuerydslBinderCustomizerDefaults bean holding default Querydsl bindings before applying specific bindings from the repository or @QuerydslPredicate .
|
9.8.3. Repository Populators
If you work with the Spring JDBC module, you are probably familiar with the support for populating a DataSource
with 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.
Assume you have a file called data.json
with the following content:
[ { "_class" : "com.acme.Person",
"firstname" : "Dave",
"lastname" : "Matthews" },
{ "_class" : "com.acme.Person",
"firstname" : "Carter",
"lastname" : "Beauford" } ]
You can 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
, declare a populator similar to the following:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/repository
https://www.springframework.org/schema/data/repository/spring-repository.xsd">
<repository:jackson2-populator locations="classpath:data.json" />
</beans>
The preceding declaration causes the data.json
file to be read and deserialized by a Jackson ObjectMapper
.
The type to which the JSON object is unmarshalled is determined by inspecting the _class
attribute of the JSON document.
The infrastructure eventually selects the appropriate repository to handle the object that was deserialized.
To instead use XML to define the data the repositories should be populated with, you can use the unmarshaller-populator
element.
You configure it to use one of the XML marshaller options available in Spring OXM. See the Spring reference documentation for details.
The following example shows how to unmarshall a repository populator with JAXB:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/repository
https://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/oxm
https://www.springframework.org/schema/oxm/spring-oxm.xsd">
<repository:unmarshaller-populator locations="classpath:data.json"
unmarshaller-ref="unmarshaller" />
<oxm:jaxb2-marshaller contextPath="com.acme" />
</beans>
Reference Documentation
10. Introduction
10.1. Document Structure
This part of the reference documentation explains the core functionality offered by Spring Data MongoDB.
“MongoDB support” introduces the MongoDB module feature set.
“MongoDB Repositories” introduces the repository support for MongoDB.
11. MongoDB support
The MongoDB support contains a wide range of features:
-
Spring configuration support with Java-based
@Configuration
classes or an XML namespace for a Mongo driver instance and replica sets. -
MongoTemplate
helper class that increases productivity when performing common Mongo operations.Includes integrated object mapping between documents and POJOs. -
Exception translation into Spring’s portable Data Access Exception hierarchy.
-
Feature-rich Object Mapping integrated with Spring’s Conversion Service.
-
Annotation-based mapping metadata that is extensible to support other metadata formats.
-
Persistence and mapping lifecycle events.
-
Java-based Query, Criteria, and Update DSLs.
-
Automatic implementation of Repository interfaces, including support for custom finder methods.
-
QueryDSL integration to support type-safe queries.
-
Cross-store persistence support for JPA Entities with fields transparently persisted and retrieved with MongoDB (deprecated - to be removed without replacement).
-
GeoSpatial integration.
For most tasks, you should use MongoTemplate
or the Repository support, which 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 the low-level API artifacts, such as com.mongodb.client.MongoDatabase
, 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.
11.1. Getting Started
An easy way to bootstrap setting up a working environment is to create a Spring-based project in STS.
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 running 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, and press Yes when prompted. Then enter a project and a package name, such as
org.spring.mongodb.example
. -
Add the following to the pom.xml files
dependencies
element:<dependencies> <!-- other dependency elements omitted --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>3.3.6</version> </dependency> </dependencies>
-
Change the version of Spring in the pom.xml to be
<spring.framework.version>5.3.22</spring.framework.version>
-
Add the following location of the Spring Milestone repository for Maven to your
pom.xml
such that it is at the same level of your<dependencies/>
element:<repositories> <repository> <id>spring-milestone</id> <name>Spring Maven MILESTONE Repository</name> <url>https://repo.spring.io/libs-milestone</url> </repository> </repositories>
The repository is also browseable here.
You may also want to set the logging level to DEBUG
to see some additional information. To do so, edit the log4j.properties
file to have the following content:
log4j.category.org.springframework.data.mongodb=DEBUG
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n
Then you can create a Person
class to persist:
package org.spring.mongodb.example;
public class Person {
private String id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
You also need a main application to run:
package org.spring.mongodb.example;
import static org.springframework.data.mongodb.core.query.Criteria.where;
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.client.MongoClients;
public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class);
public static void main(String[] args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "database");
mongoOps.insert(new Person("Joe", 34));
log.info(mongoOps.findOne(new Query(where("name").is("Joe")), Person.class));
mongoOps.dropCollection("person");
}
}
When you run the main program, the preceding examples produce the following output:
10:01:32,062 DEBUG apping.MongoPersistentEntityIndexCreator: 80 - Analyzing class class org.spring.example.Person for index information.
10:01:32,265 DEBUG ramework.data.mongodb.core.MongoTemplate: 631 - insert Document containing fields: [_class, age, name] in collection: Person
10:01:32,765 DEBUG ramework.data.mongodb.core.MongoTemplate:1243 - findOne using query: { "name" : "Joe"} in db.collection: database.Person
10:01:32,953 INFO org.spring.mongodb.example.MongoApp: 25 - Person [id=4ddbba3c0be56b7e1b210166, name=Joe, age=34]
10:01:32,984 DEBUG ramework.data.mongodb.core.MongoTemplate: 375 - Dropped collection [database.person]
Even in this simple example, there are few things to notice:
-
You can instantiate the central helper class of Spring Mongo,
MongoTemplate
, by using the standardcom.mongodb.client.MongoClient
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 anObjectId
when stored in the database. -
Mapping conventions can use field access. Notice that the
Person
class has only getters. -
If the constructor argument names match the field names of the stored document, they are used to instantiate the object
11.2. Examples Repository
There is a GitHub repository with several examples that you can download and play around with to get a feel for how the library works.
11.3. Connecting to MongoDB with Spring
One of the first tasks when using MongoDB and Spring is to create a com.mongodb.client.MongoClient
object using the IoC container. There are two main ways to do this, either by using Java-based bean metadata or by using XML-based bean metadata. Both are discussed in the following sections.
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. |
11.3.1. Registering a Mongo Instance by using Java-based Metadata
The following example shows an example of using Java-based bean metadata to register an instance of a com.mongodb.client.MongoClient
:
com.mongodb.client.MongoClient
object using Java-based bean metadata@Configuration
public class AppConfig {
/*
* Use the standard Mongo driver API to create a com.mongodb.client.MongoClient instance.
*/
public @Bean MongoClient mongoClient() {
return MongoClients.create("mongodb://localhost:27017");
}
}
This approach lets you use the standard com.mongodb.client.MongoClient
instance, with the container using Spring’s MongoClientFactoryBean
. As compared to instantiating a com.mongodb.client.MongoClient
instance directly, the FactoryBean
has the added advantage of also providing the container with an ExceptionTranslator
implementation that translates MongoDB exceptions to exceptions in Spring’s portable DataAccessException
hierarchy for data access classes annotated with the @Repository
annotation. This hierarchy and the use of @Repository
is described in Spring’s DAO support features.
The following example shows an example of a Java-based bean metadata that supports exception translation on @Repository
annotated classes:
com.mongodb.client.MongoClient
object by using Spring’s MongoClientFactoryBean
and enabling Spring’s exception translation support@Configuration
public class AppConfig {
/*
* Factory bean that creates the com.mongodb.client.MongoClient instance
*/
public @Bean MongoClientFactoryBean mongo() {
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
mongo.setHost("localhost");
return mongo;
}
}
To access the com.mongodb.client.MongoClient
object created by the MongoClientFactoryBean
in other @Configuration
classes or your own classes, use a private @Autowired MongoClient mongoClient;
field.
11.3.2. Registering a Mongo Instance by Using XML-based Metadata
While you can use Spring’s traditional <beans/>
XML namespace to register an instance of com.mongodb.client.MongoClient
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 lets you create a Mongo instance server location, replica-sets, and options.
To use the Mongo namespace elements, you need to reference the Mongo schema, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=
"
http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo-client host="localhost" port="27017"/>
</beans>
The following example shows a more advanced configuration with MongoClientSettings
(note that these are not recommended values):
com.mongodb.client.MongoClient
object with MongoClientSettings
<beans>
<mongo:mongo-client host="localhost" port="27017">
<mongo:client-settings connection-pool-max-connection-life-time="10"
connection-pool-min-size="10"
connection-pool-max-size="20"
connection-pool-maintenance-frequency="10"
connection-pool-maintenance-initial-delay="11"
connection-pool-max-connection-idle-time="30"
connection-pool-max-wait-time="15" />
</mongo:mongo-client>
</beans>
The following example shows a configuration using replica sets:
com.mongodb.client.MongoClient
object with Replica Sets<mongo:mongo-client id="replicaSetMongo" replica-set="rs0">
<mongo:client-settings cluster-hosts="127.0.0.1:27017,localhost:27018" />
</mongo:mongo-client>
11.3.3. The MongoDatabaseFactory Interface
While com.mongodb.client.MongoClient
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.client.MongoDatabase
object and access all the functionality of a specific MongoDB database instance. Spring provides the org.springframework.data.mongodb.core.MongoDatabaseFactory
interface, shown in the following listing, to bootstrap connectivity to the database:
public interface MongoDatabaseFactory {
MongoDatabase getDatabase() throws DataAccessException;
MongoDatabase getDatabase(String dbName) throws DataAccessException;
}
The following sections show how you can use the container with either Java-based or XML-based metadata to configure an instance of the MongoDatabaseFactory
interface. In turn, you can use the MongoDatabaseFactory
instance to configure MongoTemplate
.
Instead of using the IoC container to create an instance of MongoTemplate, you can use them in standard Java code, as follows:
public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class);
public static void main(String[] args) throws Exception {
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database"));
mongoOps.insert(new Person("Joe", 34));
log.info(mongoOps.findOne(new Query(where("name").is("Joe")), Person.class));
mongoOps.dropCollection("person");
}
}
The code in bold highlights the use of SimpleMongoClientDbFactory
and is the only difference between the listing shown in the getting started section.
Use SimpleMongoClientDbFactory when choosing com.mongodb.client.MongoClient as the entrypoint of choice.
|
11.3.4. Registering a MongoDatabaseFactory
Instance by Using Java-based Metadata
To register a MongoDatabaseFactory
instance with the container, you write code much like what was highlighted in the previous code listing. The following listing shows a simple example:
@Configuration
public class MongoConfiguration {
public @Bean MongoDatabaseFactory mongoDatabaseFactory() {
return new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database");
}
}
MongoDB Server generation 3 changed the authentication model when connecting to the DB. Therefore, some of the configuration options available for authentication are no longer valid. You should use the MongoClient
-specific options for setting credentials through MongoCredential
to provide authentication data, as shown in the following example:
@Configuration
public class ApplicationContextEventTestsAppConfig extends AbstractMongoClientConfiguration {
@Override
public String getDatabaseName() {
return "database";
}
@Override
protected void configureClientSettings(Builder builder) {
builder
.credential(MongoCredential.createCredential("name", "db", "pwd".toCharArray()))
.applyToClusterSettings(settings -> {
settings.hosts(singletonList(new ServerAddress("127.0.0.1", 27017)));
});
}
}
In order to use authentication with XML-based configuration, use the credential
attribute on the <mongo-client>
element.
Username and password credentials used in XML-based configuration must be URL-encoded when these contain reserved characters, such as : , % , @ , or , .
The following example shows encoded credentials:
m0ng0@dmin:mo_res:bw6},Qsdxx@admin@database → m0ng0%40dmin:mo_res%3Abw6%7D%2CQsdxx%40admin@database
See section 2.2 of RFC 3986 for further details.
|
11.3.5. Registering a MongoDatabaseFactory
Instance by Using XML-based Metadata
The mongo
namespace provides a convenient way to create a SimpleMongoClientDbFactory
, as compared to using the <beans/>
namespace, as shown in the following example:
<mongo:db-factory dbname="database">
If you need to configure additional options on the com.mongodb.client.MongoClient
instance that is used to create a SimpleMongoClientDbFactory
, you can refer to an existing bean by using the mongo-ref
attribute as shown in the following example. To show another common usage pattern, the following listing shows the use of a property placeholder, which lets you parametrize the configuration and the creation of a MongoTemplate
:
<context:property-placeholder location="classpath:/com/myapp/mongodb/config/mongo.properties"/>
<mongo:mongo-client host="${mongo.host}" port="${mongo.port}">
<mongo:client-settings connection-pool-max-connection-life-time="${mongo.pool-max-life-time}"
connection-pool-min-size="${mongo.pool-min-size}"
connection-pool-max-size="${mongo.pool-max-size}"
connection-pool-maintenance-frequency="10"
connection-pool-maintenance-initial-delay="11"
connection-pool-max-connection-idle-time="30"
connection-pool-max-wait-time="15" />
</mongo:mongo-client>
<mongo:db-factory dbname="database" mongo-ref="mongoClient"/>
<bean id="anotherMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>
11.4. Introduction to MongoTemplate
The MongoTemplate
class, located in the org.springframework.data.mongodb.core
package, is the central class of Spring’s MongoDB support and provides a rich feature set for interacting with the database. The template offers convenience operations to create, update, delete, and query MongoDB documents and provides a mapping between your domain objects and MongoDB documents.
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 MongoConverter
interface. Spring provides MappingMongoConverter
, but you can also write your own converter. See “Custom Conversions - Overriding Default Mapping” for more detailed information.
The MongoTemplate
class implements the interface MongoOperations
. In as much as possible, the methods on MongoOperations
are named after methods available on the MongoDB driver Collection
object, to make the API familiar to existing MongoDB developers who are used to the driver API. For example, you can find methods such as find
, findAndModify
, findAndReplace
, findOne
, insert
, remove
, save
, update
, and updateMulti
. The design goal was to make it as easy as possible to transition between the use of the base MongoDB driver and MongoOperations
. A major difference between the two APIs is that MongoOperations
can be passed domain objects instead of Document
. Also, MongoOperations
has fluent APIs for Query
, Criteria
, and Update
operations instead of populating a Document
to specify the parameters for those operations.
The preferred way to reference the operations on MongoTemplate instance is through its interface, MongoOperations .
|
The default converter implementation used by MongoTemplate
is MappingMongoConverter
. While the MappingMongoConverter
can use additional metadata to specify the mapping of objects to documents, it can also convert objects that contain no additional metadata by using some conventions for the mapping of IDs and collection names. These conventions, as well as the use of mapping annotations, are explained in the “Mapping” chapter.
Another central feature of MongoTemplate
is translation of exceptions thrown by the MongoDB Java driver into Spring’s portable Data Access Exception hierarchy. See “Exception Translation” for more information.
MongoTemplate
offers many convenience methods to help you easily perform common tasks. However, if you need to directly access the MongoDB driver API, you can use one of several Execute
callback methods. The execute
callbacks gives you a reference to either a com.mongodb.client.MongoCollection
or a com.mongodb.client.MongoDatabase
object. See the “Execution Callbacks” section for more information.
The next section contains an example of how to work with the MongoTemplate
in the context of the Spring container.
11.4.1. Instantiating MongoTemplate
You can use Java to create and register an instance of MongoTemplate
, as the following example shows:
com.mongodb.client.MongoClient
object and enabling Spring’s exception translation support@Configuration
public class AppConfig {
public @Bean MongoClient mongoClient() {
return MongoClients.create("mongodb://localhost:27017");
}
public @Bean MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient(), "mydatabase");
}
}
There are several overloaded constructors of MongoTemplate
:
-
MongoTemplate(MongoClient mongo, String databaseName)
: Takes theMongoClient
object and the default database name to operate against. -
MongoTemplate(MongoDatabaseFactory mongoDbFactory)
: Takes a MongoDbFactory object that encapsulated theMongoClient
object, database name, and username and password. -
MongoTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter mongoConverter)
: Adds aMongoConverter
to use for mapping.
You can also configure a MongoTemplate by using Spring’s XML <beans/> schema, as the following example shows:
<mongo:mongo-client host="localhost" port="27017"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoClient"/>
<constructor-arg name="databaseName" value="geospatial"/>
</bean>
Other optional properties that you might like to set when creating a MongoTemplate
are the default WriteResultCheckingPolicy
, WriteConcern
, and ReadPreference
properties.
The preferred way to reference the operations on MongoTemplate instance is through its interface, MongoOperations .
|
11.4.2. WriteResultChecking
Policy
When in development, it is handy to either log or throw an exception if the com.mongodb.WriteResult
returned from any MongoDB operation contains an error. It is quite common to forget to do this during development and then end up with an application that looks like it runs successfully when, in fact, the database was not modified according to your expectations. You can set the WriteResultChecking
property of MongoTemplate
to one of the following values: EXCEPTION
or NONE
, to either throw an Exception
or do nothing, respectively. The default is to use a WriteResultChecking
value of NONE
.
11.4.3. WriteConcern
If it has not yet been specified through the driver at a higher level (such as com.mongodb.client.MongoClient
), you can set the com.mongodb.WriteConcern
property that the MongoTemplate
uses for write operations. If the WriteConcern
property is not set, it defaults to the one set in the MongoDB driver’s DB or Collection setting.
11.4.4. 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 following listing shows the WriteConcernResolver
interface:
public interface WriteConcernResolver {
WriteConcern resolve(MongoAction action);
}
You can use the MongoAction
argument to determine the WriteConcern
value or 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 Document
, the operation (REMOVE
, UPDATE
, INSERT
, INSERT_LIST
, or SAVE
), and a few other pieces of contextual information. The following example shows two sets of classes getting different WriteConcern
settings:
private class MyAppWriteConcernResolver implements WriteConcernResolver {
public WriteConcern resolve(MongoAction action) {
if (action.getEntityClass().getSimpleName().contains("Audit")) {
return WriteConcern.NONE;
} else if (action.getEntityClass().getSimpleName().contains("Metadata")) {
return WriteConcern.JOURNAL_SAFE;
}
return action.getDefaultWriteConcern();
}
}
11.5. Saving, Updating, and Removing Documents
MongoTemplate
lets you save, update, and delete your domain objects and map those objects to documents stored in MongoDB.
Consider the following class:
public class Person {
private String id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
Given the Person
class in the preceding example, you can save, update and delete the object, as the following example shows:
MongoOperations is the interface that MongoTemplate implements.
|
package org.spring.example;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Update.update;
import static org.springframework.data.mongodb.core.query.Query.query;
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.SimpleMongoClientDbFactory;
import com.mongodb.client.MongoClients;
public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class);
public static void main(String[] args) {
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDbFactory(MongoClients.create(), "database"));
Person p = new Person("Joe", 34);
// Insert is used to initially store the object into the database.
mongoOps.insert(p);
log.info("Insert: " + p);
// 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);
// Check that deletion worked
List<Person> people = mongoOps.findAll(Person.class);
log.info("Number of people = : " + people.size());
mongoOps.dropCollection(Person.class);
}
}
The preceding example would produce the following log output (including debug messages from MongoTemplate
):
DEBUG apping.MongoPersistentEntityIndexCreator: 80 - Analyzing class class org.spring.example.Person for index information.
DEBUG work.data.mongodb.core.MongoTemplate: 632 - insert Document containing fields: [_class, age, name] in collection: person
INFO org.spring.example.MongoApp: 30 - Insert: Person [id=4ddc6e784ce5b1eba3ceaf5c, name=Joe, age=34]
DEBUG work.data.mongodb.core.MongoTemplate:1246 - findOne using query: { "_id" : { "$oid" : "4ddc6e784ce5b1eba3ceaf5c"}} in db.collection: database.person
INFO org.spring.example.MongoApp: 34 - Found: Person [id=4ddc6e784ce5b1eba3ceaf5c, name=Joe, age=34]
DEBUG work.data.mongodb.core.MongoTemplate: 778 - calling update using query: { "name" : "Joe"} and update: { "$set" : { "age" : 35}} in collection: person
DEBUG work.data.mongodb.core.MongoTemplate:1246 - findOne using query: { "name" : "Joe"} in db.collection: database.person
INFO org.spring.example.MongoApp: 39 - Updated: Person [id=4ddc6e784ce5b1eba3ceaf5c, name=Joe, age=35]
DEBUG work.data.mongodb.core.MongoTemplate: 823 - remove using query: { "id" : "4ddc6e784ce5b1eba3ceaf5c"} in collection: person
INFO org.spring.example.MongoApp: 46 - Number of people = : 0
DEBUG work.data.mongodb.core.MongoTemplate: 376 - Dropped collection [database.person]
MongoConverter
caused implicit conversion between a String
and an ObjectId
stored in the database by recognizing (through convention) the Id
property name.
The preceding 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 preceding example is explained in more detail in the section “Querying Documents”.
11.5.1. How the _id
Field is Handled in the Mapping Layer
MongoDB requires that you have an _id
field for all documents. If you do not provide one, the driver assigns an ObjectId
with a generated value. When you use the MappingMongoConverter
, certain rules govern how properties from the Java class are mapped to this _id
field:
-
A property or field annotated with
@Id
(org.springframework.data.annotation.Id
) maps to the_id
field. -
A property or field without an annotation but named
id
maps to the_id
field.
The following outlines what type conversion, if any, is done on the property mapped to the _id
document field when using the MappingMongoConverter
(the default for MongoTemplate
).
-
If possible, an
id
property or field declared as aString
in the Java class is converted to and stored as anObjectId
by using a SpringConverter<String, ObjectId>
. Valid conversion rules are delegated to the MongoDB Java driver. If it cannot be converted to anObjectId
, then the value is stored as a string in the database. -
An
id
property or field declared asBigInteger
in the Java class is converted to and stored as anObjectId
by using a SpringConverter<BigInteger, ObjectId>
.
If no field or property specified in the previous sets of rules is present in the Java class, an implicit _id
file is generated by the driver but not mapped to a property or field of the Java class.
When querying and updating, MongoTemplate
uses the converter that corresponds to the preceding rules for saving documents so that field names and types used in your queries can match what is in your domain classes.
Some environments require a customized approach to map Id
values such as data stored in MongoDB that did not run through the Spring Data mapping layer. Documents can contain _id
values that can be represented either as ObjectId
or as String
.
Reading documents from the store back to the domain type works just fine. Querying for documents via their id
can be cumbersome due to the implicit ObjectId
conversion. Therefore documents cannot be retrieved that way.
For those cases @MongoId
provides more control over the actual id mapping attempts.
@MongoId
mappingpublic class PlainStringId {
@MongoId String id; (1)
}
public class PlainObjectId {
@MongoId ObjectId id; (2)
}
public class StringToObjectId {
@MongoId(FieldType.OBJECT_ID) String id; (3)
}
1 | The id is treated as String without further conversion. |
2 | The id is treated as ObjectId . |
3 | The id is treated as ObjectId if the given String is a valid ObjectId hex, otherwise as String . Corresponds to @Id usage. |
11.5.2. Type Mapping
MongoDB collections can contain documents that represent instances of a variety of types.This feature can be useful if you store a hierarchy of classes or 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.
To achieve that, the MappingMongoConverter
uses a MongoTypeMapper
abstraction with DefaultMongoTypeMapper
as its main implementation.Its default behavior to store the fully qualified classname under _class
inside the document.Type hints are written for top-level documents as well as for every value (if it is a complex type and a subtype of the declared property type).The following example (with a JSON representation at the end) shows how the mapping works:
public class Sample {
Contact value;
}
public abstract class Contact { … }
public class Person extends Contact { … }
Sample sample = new Sample();
sample.value = new Person();
mongoTemplate.save(sample);
{
"value" : { "_class" : "com.acme.Person" },
"_class" : "com.acme.Sample"
}
Spring Data MongoDB stores the type information as the last field for the actual root class as well as for the nested type (because it is complex and a subtype of Contact
).So, if you now use mongoTemplate.findAll(Object.class, "sample")
, you can find out that the document stored is a Sample
instance.You can also find out that the value property is actually a Person
.
Customizing Type Mapping
If you want to avoid writing the entire Java class name as type information but would rather like to use a key, you can use the @TypeAlias
annotation on the entity class.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, in turn, be configured on MappingMongoConverter
.The following example shows how to define a type alias for an entity:
@TypeAlias("pers")
class Person {
}
Note that the resulting document contains pers
as the value in the _class
Field.
Type aliases only work if the mapping context is aware of the actual type. The required entity metadata is determined either on first save or has to be provided via the configurations initial entity set. By default, the configuration class scans the base package for potential candidates.
|
Configuring Custom Type Mapping
The following example shows how to configure a custom MongoTypeMapper
in MappingMongoConverter
:
MongoTypeMapper
with Spring Java Configclass CustomMongoTypeMapper extends DefaultMongoTypeMapper {
//implement custom type mapping here
}
@Configuration
class SampleMongoConfiguration extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "database";
}
@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 the preceding example extends the AbstractMongoClientConfiguration
class and overrides the bean definition of the MappingMongoConverter
where we configured our custom MongoTypeMapper
.
The following example shows how to use XML to configure a custom MongoTypeMapper
:
MongoTypeMapper
with XML<mongo:mapping-converter type-mapper-ref="customMongoTypeMapper"/>
<bean name="customMongoTypeMapper" class="com.bubu.mongo.CustomMongoTypeMapper"/>
11.5.3. Methods for Saving and Inserting Documents
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, Document>
and Converter<Document, Person>
.
The difference between insert and save operations is that a save operation performs 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 is determined by name (not fully qualified) of the class. You may also call the save operation with a specific collection name. You can use mapping metadata to override the collection in which to store the object.
When inserting or saving, if the Id
property is not set, the assumption is that its value will be auto-generated by the database. Consequently, for auto-generation of an ObjectId
to succeed, the type of the Id
property or field in your class must be a String
, an ObjectId
, or a BigInteger
.
The following example shows how to save a document and retrieving its contents:
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Criteria.query;
…
Person p = new Person("Bob", 33);
mongoTemplate.insert(p);
Person qp = mongoTemplate.findOne(query(where("age").is(33)), Person.class);
The following insert and save operations are available:
-
void
save(Object objectToSave)
: Save the object to the default collection. -
void
save(Object objectToSave, String collectionName)
: Save the object to the specified collection.
A similar set of insert operations is also available:
-
void
insert(Object objectToSave)
: Insert the object to the default collection. -
void
insert(Object objectToSave, String collectionName)
: Insert the object to the specified collection.
Into Which Collection Are My Documents Saved?
There are two ways to manage the collection name that is used for 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 is stored in the person
collection. You can customize this by providing a different collection name with 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.
Inserting or Saving Individual Objects
The MongoDB driver supports inserting a collection of documents in a single operation. The following methods in the MongoOperations
interface support this functionality:
-
insert: Inserts an object. If there is an existing document with the same
id
, an error is generated. -
insertAll: Takes a
Collection
of objects as the first parameter. This method inspects each object and inserts it into the appropriate collection, based on the rules specified earlier. -
save: Saves the object, overwriting any object that might have the same
id
.
Inserting Several Objects in a Batch
The MongoDB driver supports inserting a collection of documents in one operation. The following methods in the MongoOperations
interface support this functionality:
-
insert methods: Take a
Collection
as the first argument. They insert a list of objects in a single batch write to the database.
11.5.4. Updating Documents in a Collection
For updates, you can update the first document found by using MongoOperation.updateFirst
or you can update all documents that were found to match the query by using the MongoOperation.updateMulti
method. The following example shows an update of all SAVINGS
accounts where we are adding a one-time $50.00 bonus to the balance by using the $inc
operator:
MongoTemplate
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query;
import static org.springframework.data.mongodb.core.query.Update;
...
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 earlier, we provide the update definition by using an Update
object. The Update
class has methods that match the update modifiers available for MongoDB.
Most methods return the Update
object to provide a fluent style for the API.
Methods for Running Updates for Documents
-
updateFirst: Updates the first document that matches the query document criteria with the updated document.
-
updateMulti: Updates all objects that match the query document criteria with the updated document.
updateFirst does not support ordering. Please use findAndModify to apply Sort .
|
Methods in the Update
Class
You can use a little "'syntax sugar'" with the Update
class, as its methods are meant to be chained together. Also, you can kick-start the creation of a new Update
instance by using public static Update update(String key, Object value)
and using static imports.
The Update
class contains the following methods:
-
Update
addToSet(String key, Object value)
Update using the$addToSet
update modifier -
Update
currentDate(String key)
Update using the$currentDate
update modifier -
Update
currentTimestamp(String key)
Update using the$currentDate
update modifier with$type
timestamp
-
Update
inc(String key, Number inc)
Update using the$inc
update modifier -
Update
max(String key, Object max)
Update using the$max
update modifier -
Update
min(String key, Object min)
Update using the$min
update modifier -
Update
multiply(String key, Number multiplier)
Update using the$mul
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
setOnInsert(String key, Object value)
Update using the$setOnInsert
update modifier -
Update
unset(String key)
Update using the$unset
update modifier
Some update modifiers, such as $push
and $addToSet
, allow nesting of additional operators.
// { $push : { "category" : { "$each" : [ "spring" , "data" ] } } }
new Update().push("category").each("spring", "data")
// { $push : { "key" : { "$position" : 0 , "$each" : [ "Arya" , "Arry" , "Weasel" ] } } }
new Update().push("key").atPosition(Position.FIRST).each(Arrays.asList("Arya", "Arry", "Weasel"));
// { $push : { "key" : { "$slice" : 5 , "$each" : [ "Arya" , "Arry" , "Weasel" ] } } }
new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel"));
// { $addToSet : { "values" : { "$each" : [ "spring" , "data" , "mongodb" ] } } }
new Update().addToSet("values").each("spring", "data", "mongodb");
11.5.5. “Upserting” Documents in a Collection
Related to performing an updateFirst
operation, 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. The following example shows how to use the upsert
method:
template.update(Person.class)
.matching(query(where("ssn").is(1111).and("firstName").is("Joe").and("Fraizer").is("Update"))
.apply(update("address", addr))
.upsert();
upsert does not support ordering. Please use findAndModify to apply Sort .
|
11.5.6. Finding and Upserting Documents in a Collection
The findAndModify(…)
method on MongoCollection
can update a document and return either the old or newly updated document in a single operation. MongoTemplate
provides four findAndModify
overloaded methods that take Query
and Update
classes and converts from Document
to your POJOs:
<T> T findAndModify(Query query, Update update, Class<T> entityClass);
<T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName);
<T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass);
<T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass, String collectionName);
The following example inserts a few Person
objects into the container and performs a findAndUpdate
operation:
template.insert(new Person("Tom", 21));
template.insert(new Person("Dick", 22));
template.insert(new Person("Harry", 23));
Query query = new Query(Criteria.where("firstName").is("Harry"));
Update update = new Update().inc("age", 1);
Person oldValue = template.update(Person.class)
.matching(query)
.apply(update)
.findAndModifyValue(); // return's old person object
assertThat(oldValue.getFirstName()).isEqualTo("Harry");
assertThat(oldValue.getAge()).isEqualTo(23);
Person newValue = template.query(Person.class)
.matching(query)
.findOneValue();
assertThat(newValue.getAge()).isEqualTo(24);
Person newestValue = template.update(Person.class)
.matching(query)
.apply(update)
.withOptions(FindAndModifyOptions.options().returnNew(true)) // Now return the newly updated document when updating
.findAndModifyValue();
assertThat(newestValue.getAge()).isEqualTo(25);
The FindAndModifyOptions
method lets you set the options of returnNew
, upsert
, and remove
.An example extending from the previous code snippet follows:
Person upserted = template.update(Person.class)
.matching(new Query(Criteria.where("firstName").is("Mary")))
.apply(update)
.withOptions(FindAndModifyOptions.options().upsert(true).returnNew(true))
.findAndModifyValue()
assertThat(upserted.getFirstName()).isEqualTo("Mary");
assertThat(upserted.getAge()).isOne();
11.5.7. Aggregation Pipeline Updates
Update methods exposed by MongoOperations
and ReactiveMongoOperations
also accept an Aggregation Pipeline via AggregationUpdate
.
Using AggregationUpdate
allows leveraging MongoDB 4.2 aggregations in an update operation.
Using aggregations in an update allows updating one or more fields by expressing multiple stages and multiple conditions with a single operation.
The update can consist of the following stages:
-
AggregationUpdate.set(…).toValue(…)
→$set : { … }
-
AggregationUpdate.unset(…)
→$unset : [ … ]
-
AggregationUpdate.replaceWith(…)
→$replaceWith : { … }
AggregationUpdate update = Aggregation.newUpdate()
.set("average").toValue(ArithmeticOperators.valueOf("tests").avg()) (1)
.set("grade").toValue(ConditionalOperators.switchCases( (2)
when(valueOf("average").greaterThanEqualToValue(90)).then("A"),
when(valueOf("average").greaterThanEqualToValue(80)).then("B"),
when(valueOf("average").greaterThanEqualToValue(70)).then("C"),
when(valueOf("average").greaterThanEqualToValue(60)).then("D"))
.defaultTo("F")
);
template.update(Student.class) (3)
.apply(update)
.all(); (4)
db.students.update( (3)
{ },
[
{ $set: { average : { $avg: "$tests" } } }, (1)
{ $set: { grade: { $switch: { (2)
branches: [
{ case: { $gte: [ "$average", 90 ] }, then: "A" },
{ case: { $gte: [ "$average", 80 ] }, then: "B" },
{ case: { $gte: [ "$average", 70 ] }, then: "C" },
{ case: { $gte: [ "$average", 60 ] }, then: "D" }
],
default: "F"
} } } }
],
{ multi: true } (4)
)
1 | The 1st $set stage calculates a new field average based on the average of the tests field. |
2 | The 2nd $set stage calculates a new field grade based on the average field calculated by the first aggregation stage. |
3 | The pipeline is run on the students collection and uses Student for the aggregation field mapping. |
4 | Apply the update to all matching documents in the collection. |
11.5.8. Finding and Replacing Documents
The most straight forward method of replacing an entire Document
is via its id
using the save
method. However this
might not always be feasible. findAndReplace
offers an alternative that allows to identify the document to replace via
a simple query.
Optional<User> result = template.update(Person.class) (1)
.matching(query(where("firstame").is("Tom"))) (2)
.replaceWith(new Person("Dick"))
.withOptions(FindAndReplaceOptions.options().upsert()) (3)
.as(User.class) (4)
.findAndReplace(); (5)
1 | Use the fluent update API with the domain type given for mapping the query and deriving the collection name or just use MongoOperations#findAndReplace . |
2 | The actual match query mapped against the given domain type. Provide sort , fields and collation settings via the query. |
3 | Additional optional hook to provide options other than the defaults, like upsert . |
4 | An optional projection type used for mapping the operation result. If none given the initial domain type is used. |
5 | Trigger the actual processing. Use findAndReplaceValue to obtain the nullable result instead of an Optional . |
Please note that the replacement must not hold an id itself as the id of the existing Document will be
carried over to the replacement by the store itself. Also keep in mind that findAndReplace will only replace the first
document matching the query criteria depending on a potentially given sort order.
|
11.5.9. Methods for Removing Documents
You can use one of five overloaded methods to remove an object from the database:
template.remove(tywin, "GOT"); (1)
template.remove(query(where("lastname").is("lannister")), "GOT"); (2)
template.remove(new Query().limit(3), "GOT"); (3)
template.findAllAndRemove(query(where("lastname").is("lannister"), "GOT"); (4)
template.findAllAndRemove(new Query().limit(3), "GOT"); (5)
1 | Remove a single entity specified by its _id from the associated collection. |
2 | Remove all documents that match the criteria of the query from the GOT collection. |
3 | Remove the first three documents in the GOT collection. Unlike <2>, the documents to remove are identified by their _id , running the given query, applying sort , limit , and skip options first, and then removing all at once in a separate step. |
4 | Remove all documents matching the criteria of the query from the GOT collection. Unlike <3>, documents do not get deleted in a batch but one by one. |
5 | Remove the first three documents in the GOT collection. Unlike <3>, documents do not get deleted in a batch but one by one. |
11.5.10. Optimistic Locking
The @Version
annotation provides syntax similar to that of JPA in the context of MongoDB and makes sure updates are only applied to documents with a matching version. Therefore, the actual value of the version property is added to the update query in such a way that the update does not have any effect if another operation altered the document in the meantime. In that case, an OptimisticLockingFailureException
is thrown. The following example shows these features:
@Document
class Person {
@Id String id;
String firstname;
String lastname;
@Version Long version;
}
Person daenerys = template.insert(new Person("Daenerys")); (1)
Person tmp = template.findOne(query(where("id").is(daenerys.getId())), Person.class); (2)
daenerys.setLastname("Targaryen");
template.save(daenerys); (3)
template.save(tmp); // throws OptimisticLockingFailureException (4)
1 | Intially insert document. version is set to 0 . |
2 | Load the just inserted document. version is still 0 . |
3 | Update the document with version = 0 . Set the lastname and bump version to 1 . |
4 | Try to update the previously loaded document that still has version = 0 . The operation fails with an OptimisticLockingFailureException , as the current version is 1 . |
Optimistic Locking requires to set the WriteConcern to ACKNOWLEDGED . Otherwise OptimisticLockingFailureException can be silently swallowed.
|
As of Version 2.2 MongoOperations also includes the @Version property when removing an entity from the database.
To remove a Document without version check use MongoOperations#remove(Query,…) instead of MongoOperations#remove(Object) .
|
As of Version 2.2 repositories check for the outcome of acknowledged deletes when removing versioned entities.
An OptimisticLockingFailureException is raised if a versioned entity cannot be deleted through CrudRepository.delete(Object) . In such case, the version was changed or the object was deleted in the meantime. Use CrudRepository.deleteById(ID) to bypass optimistic locking functionality and delete objects regardless of their version.
|
11.6. Querying Documents
You can use the Query
and Criteria
classes to express your queries.They have method names that mirror the native MongoDB operator names, such as lt
, lte
, is
, and others.The Query
and Criteria
classes follow a fluent API style so that you can chain together multiple method criteria and queries while having easy-to-understand code.To improve readability, static imports let you avoid using the 'new' keyword for creating Query
and Criteria
instances.You can also use BasicQuery
to create Query
instances from plain JSON Strings, as shown in the following example:
BasicQuery query = new BasicQuery("{ age : { $lt : 50 }, accounts.balance : { $gt : 1000.00 }}");
List<Person> result = mongoTemplate.find(query, Person.class);
Spring MongoDB also supports GeoSpatial queries (see the GeoSpatial Queries section) and Map-Reduce operations (see the Map-Reduce section.).
11.6.1. Querying Documents in a Collection
Earlier, we saw how to retrieve a single document by using the findOne
and findById
methods on MongoTemplate
. These methods return a single domain object. We can also query for a collection of documents to be returned as a list of domain objects. Assuming that we have a number of Person
objects with name and age stored as documents in a collection and that each person has an embedded account document with a balance, we can now run a query using the following code:
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
// ...
List<Person> result = template.query(Person.class)
.matching(query(where("age").lt(50).and("accounts.balance").gt(1000.00d)))
.all();
All find methods take a Query
object as a parameter. This object defines the criteria and options used to perform the query. The criteria are specified by using a Criteria
object that has a static factory method named where
to instantiate a new Criteria
object. We recommend using static imports for org.springframework.data.mongodb.core.query.Criteria.where
and Query.query
to make the query more readable.
The query should return a list of Person
objects that meet the specified criteria. The rest of this section lists the methods of the Criteria
and Query
classes that correspond to the operators provided in MongoDB. Most methods return the Criteria
object, to provide a fluent style for the API.
Methods for the Criteria Class
The Criteria
class provides the following methods, all of which correspond to operators in MongoDB:
-
Criteria
all(Object o)
Creates a criterion using the$all
operator -
Criteria
and(String key)
Adds a chainedCriteria
with the specifiedkey
to the currentCriteria
and returns the newly created one -
Criteria
andOperator(Criteria… criteria)
Creates an and query using the$and
operator for all of the provided criteria (requires MongoDB 2.0 or later) -
Criteria
andOperator(Collection<Criteria> criteria)
Creates an and query using the$and
operator for all of the provided criteria (requires MongoDB 2.0 or later) -
Criteria
elemMatch(Criteria c)
Creates a criterion using the$elemMatch
operator -
Criteria
exists(boolean b)
Creates a criterion using the$exists
operator -
Criteria
gt(Object o)
Creates a criterion using the$gt
operator -
Criteria
gte(Object o)
Creates a criterion using the$gte
operator -
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
is(Object o)
Creates a criterion using field matching ({ key:value }
). If the specified value is a document, the order of the fields and exact equality in the document matters. -
Criteria
lt(Object o)
Creates a criterion using the$lt
operator -
Criteria
lte(Object o)
Creates a criterion using the$lte
operator -
Criteria
mod(Number value, Number remainder)
Creates a criterion using the$mod
operator -
Criteria
ne(Object o)
Creates a criterion using the$ne
operator -
Criteria
nin(Object… o)
Creates a criterion using the$nin
operator -
Criteria
norOperator(Criteria… criteria)
Creates an nor query using the$nor
operator for all of the provided criteria -
Criteria
norOperator(Collection<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 -
Criteria
orOperator(Criteria… criteria)
Creates an or query using the$or
operator for all of the provided criteria -
Criteria
orOperator(Collection<Criteria> criteria)
Creates an or query using the$or
operator for all of the provided criteria -
Criteria
regex(String re)
Creates a criterion using a$regex
-
Criteria
sampleRate(double sampleRate)
Creates a criterion using the$sampleRate
operator -
Criteria
size(int s)
Creates a criterion using the$size
operator -
Criteria
type(int t)
Creates a criterion using the$type
operator -
Criteria
matchingDocumentStructure(MongoJsonSchema schema)
Creates a criterion using the$jsonSchema
operator for JSON schema criteria.$jsonSchema
can only be applied on the top level of a query and not property specific. Use theproperties
attribute of the schema to match against nested fields. -
Criteria
bits() is the gateway to MongoDB bitwise query operators like$bitsAllClear
.
The Criteria class also provides the following methods for geospatial queries (see the GeoSpatial Queries section to see them in action):
-
Criteria
within(Circle circle)
Creates a geospatial criterion using$geoWithin $center
operators. -
Criteria
within(Box box)
Creates a geospatial criterion using a$geoWithin $box
operation. -
Criteria
withinSphere(Circle circle)
Creates a geospatial criterion using$geoWithin $center
operators. -
Criteria
near(Point point)
Creates a geospatial criterion using a$near
operation -
Criteria
nearSphere(Point point)
Creates a geospatial criterion using$nearSphere$center
operations. This is only available for MongoDB 1.7 and higher. -
Criteria
minDistance(double minDistance)
Creates a geospatial criterion using the$minDistance
operation, for use with $near. -
Criteria
maxDistance(double maxDistance)
Creates a geospatial criterion using the$maxDistance
operation, for use with $near.
Methods for the Query class
The Query
class has some additional methods that provide options for the query:
-
Query
addCriteria(Criteria criteria)
used to add additional criteria to the query -
Field
fields()
used to define fields to be included in the query results -
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) -
Query
with(Sort sort)
used to provide sort definition for the results
Selecting fields
MongoDB supports projecting fields returned by a query.
A projection can include and exclude fields (the _id
field is always included unless explicitly excluded) based on their name.
public class Person {
@Id String id;
String firstname;
@Field("last_name")
String lastname;
Address address;
}
query.fields().include("lastname"); (1)
query.fields().exclude("id").include("lastname") (2)
query.fields().include("address") (3)
query.fields().include("address.city") (4)
1 | Result will contain both _id and last_name via { "last_name" : 1 } . |
2 | Result will only contain the last_name via { "_id" : 0, "last_name" : 1 } . |
3 | Result will contain the _id and entire address object via { "address" : 1 } . |
4 | Result will contain the _id and and address object that only contains the city field via { "address.city" : 1 } . |
Starting with MongoDB 4.4 you can use aggregation expressions for field projections as shown below:
query.fields()
.project(MongoExpression.create("'$toUpper' : '$last_name'")) (1)
.as("last_name"); (2)
query.fields()
.project(StringOperators.valueOf("lastname").toUpper()) (3)
.as("last_name");
query.fields()
.project(AggregationSpELExpression.expressionOf("toUpper(lastname)")) (4)
.as("last_name");
1 | Use a native expression. The used field name must refer to field names within the database document. |
2 | Assign the field name to which the expression result is projected. The resulting field name is not mapped against the domain model. |
3 | Use an AggregationExpression . Other than native MongoExpression , field names are mapped to the ones used in the domain model. |
4 | Use SpEL along with an AggregationExpression to invoke expression functions. Field names are mapped to the ones used in the domain model. |
@Query(fields="…")
allows usage of expression field projections at Repository
level as described in MongoDB JSON-based Query Methods and Field Restriction.
11.6.2. Methods for Querying for Documents
The query methods need to specify the target type T
that is returned, and they are overloaded with an explicit collection name for queries that should operate on a collection other than the one indicated by the return type. The following query methods let you find one or more documents:
-
findAll: Query for a list of objects of type
T
from the collection. -
findOne: Map the results of an ad-hoc query on the collection to a single instance of an object of the specified type.
-
findById: Return an object of the given ID and target class.
-
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 removed from the collection in the database.
11.6.3. Query Distinct Values
MongoDB provides an operation to obtain distinct values for a single field by using a query from the resulting documents. Resulting values are not required to have the same data type, nor is the feature limited to simple types. For retrieval, the actual result type does matter for the sake of conversion and typing. The following example shows how to query for distinct values:
template.query(Person.class) (1)
.distinct("lastname") (2)
.all(); (3)
1 | Query the Person collection. |
2 | Select distinct values of the lastname field. The field name is mapped according to the domain types property declaration, taking potential @Field annotations into account. |
3 | Retrieve all distinct values as a List of Object (due to no explicit result type being specified). |
Retrieving distinct values into a Collection
of Object
is the most flexible way, as it tries to determine the property value of the domain type and convert results to the desired type or mapping Document
structures.
Sometimes, when all values of the desired field are fixed to a certain type, it is more convenient to directly obtain a correctly typed Collection
, as shown in the following example:
template.query(Person.class) (1)
.distinct("lastname") (2)
.as(String.class) (3)
.all(); (4)
1 | Query the collection of Person . |
2 | Select distinct values of the lastname field. The fieldname is mapped according to the domain types property declaration, taking potential @Field annotations into account. |
3 | Retrieved values are converted into the desired target type — in this case, String . It is also possible to map the values to a more complex type if the stored field contains a document. |
4 | Retrieve all distinct values as a List of String . If the type cannot be converted into the desired target type, this method throws a DataAccessException . |
11.6.4. GeoSpatial Queries
MongoDB supports GeoSpatial queries through the use of operators such as $near
, $within
, geoWithin
, 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.
Using GeoSpatial queries requires attention when used within MongoDB transactions, see Special behavior inside transactions. |
To understand how to perform GeoSpatial queries, consider the following Venue
class (taken from the integration tests and relying on 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;
}
public Venue(String name, double x, double y) {
super();
this.name = name;
this.location = new double[] { x, y };
}
public String getName() {
return name;
}
public double[] getLocation() {
return location;
}
@Override
public String toString() {
return "Venue [id=" + id + ", name=" + name + ", location="
+ Arrays.toString(location) + "]";
}
}
To find locations within a Circle
, you can use the following query:
Circle circle = new Circle(-73.99171, 40.738868, 0.01);
List<Venue> venues =
template.find(new Query(Criteria.where("location").within(circle)), Venue.class);
To find venues within a Circle
using spherical coordinates, you can use the following query:
Circle circle = new Circle(-73.99171, 40.738868, 0.003712240453784);
List<Venue> venues =
template.find(new Query(Criteria.where("location").withinSphere(circle)), Venue.class);
To find venues within a Box
, you can use the following query:
//lower-left then upper-right
Box box = new Box(new Point(-73.99756, 40.73083), new Point(-73.988135, 40.741404));
List<Venue> venues =
template.find(new Query(Criteria.where("location").within(box)), Venue.class);
To find venues near a Point
, you can use the following queries:
Point point = new Point(-73.99171, 40.738868);
List<Venue> venues =
template.find(new Query(Criteria.where("location").near(point).maxDistance(0.01)), Venue.class);
Point point = new Point(-73.99171, 40.738868);
List<Venue> venues =
template.find(new Query(Criteria.where("location").near(point).minDistance(0.01).maxDistance(100)), Venue.class);
To find venues near a Point
using spherical coordinates, you can use the following query:
Point point = new Point(-73.99171, 40.738868);
List<Venue> venues =
template.find(new Query(
Criteria.where("location").nearSphere(point).maxDistance(0.003712240453784)),
Venue.class);
Geo-near Queries
Changed in 2.2! Spring Data MongoDB 2.2 The calculated distance (the Target types may contain a property named after the returned distance to (additionally) read it back directly into the domain type as shown below.
|
MongoDB supports querying the database for geo locations and calculating the distance from a given origin at the same time. With geo-near queries, you can express queries such as "find all restaurants in the surrounding 10 miles". To let you do so, MongoOperations
provides geoNear(…)
methods that take a NearQuery
as an argument (as well as the already familiar entity type and collection), as shown in the following example:
Point location = new Point(-73.99171, 40.738868);
NearQuery query = NearQuery.near(location).maxDistance(new Distance(10, Metrics.MILES));
GeoResults<Restaurant> = operations.geoNear(query, Restaurant.class);
We use the NearQuery
builder API to set up a query to return all Restaurant
instances surrounding the given Point
out to 10 miles. 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 built-in metrics (miles and kilometers) automatically triggers the spherical flag to be set on the query. If you want to avoid that, pass 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. Wrapping GeoResults
allows accessing the average distance of all results. A single GeoResult
object carries the entity found plus its distance from the origin.
11.6.5. GeoJSON Support
MongoDB supports GeoJSON and simple (legacy) coordinate pairs for geospatial data. Those formats can both be used for storing as well as querying data. See the MongoDB manual on GeoJSON support to learn about requirements and restrictions.
GeoJSON Types in Domain Classes
Usage of GeoJSON types in domain classes is straightforward. The org.springframework.data.mongodb.core.geo
package contains types such as GeoJsonPoint
, GeoJsonPolygon
, and others. These types are extend the existing org.springframework.data.geo
types. The following example uses a GeoJsonPoint
:
public class Store {
String id;
/**
* location is stored in GeoJSON format.
* {
* "type" : "Point",
* "coordinates" : [ x, y ]
* }
*/
GeoJsonPoint location;
}
GeoJSON Types in Repository Query Methods
Using GeoJSON types as repository query parameters forces usage of the $geometry
operator when creating the query, as the following example shows:
public interface StoreRepository extends CrudRepository<Store, String> {
List<Store> findByLocationWithin(Polygon polygon); (1)
}
/*
* {
* "location": {
* "$geoWithin": {
* "$geometry": {
* "type": "Polygon",
* "coordinates": [
* [
* [-73.992514,40.758934],
* [-73.961138,40.760348],
* [-73.991658,40.730006],
* [-73.992514,40.758934]
* ]
* ]
* }
* }
* }
* }
*/
repo.findByLocationWithin( (2)
new GeoJsonPolygon(
new Point(-73.992514, 40.758934),
new Point(-73.961138, 40.760348),
new Point(-73.991658, 40.730006),
new Point(-73.992514, 40.758934))); (3)
/*
* {
* "location" : {
* "$geoWithin" : {
* "$polygon" : [ [-73.992514,40.758934] , [-73.961138,40.760348] , [-73.991658,40.730006] ]
* }
* }
* }
*/
repo.findByLocationWithin( (4)
new Polygon(
new Point(-73.992514, 40.758934),
new Point(-73.961138, 40.760348),
new Point(-73.991658, 40.730006)));
1 | Repository method definition using the commons type allows calling it with both the GeoJSON and the legacy format. |
2 | Use GeoJSON type to make use of $geometry operator. |
3 | Note that GeoJSON polygons need to define a closed ring. |
4 | Use the legacy format $polygon operator. |
Metrics and Distance calculation
Then MongoDB $geoNear
operator allows usage of a GeoJSON Point or legacy coordinate pairs.
NearQuery.near(new Point(-73.99171, 40.738868))
{
"$geoNear": {
//...
"near": [-73.99171, 40.738868]
}
}
NearQuery.near(new GeoJsonPoint(-73.99171, 40.738868))
{
"$geoNear": {
//...
"near": { "type": "Point", "coordinates": [-73.99171, 40.738868] }
}
}
Though syntactically different the server is fine accepting both no matter what format the target Document within the collection is using.
There is a huge difference in the distance calculation. Using the legacy format operates upon Radians on an Earth like sphere, whereas the GeoJSON format uses Meters. |
To avoid a serious headache make sure to set the Metric
to the desired unit of measure which ensures the
distance to be calculated correctly.
In other words:
Assume you’ve got 5 Documents like the ones below:
{
"_id" : ObjectId("5c10f3735d38908db52796a5"),
"name" : "Penn Station",
"location" : { "type" : "Point", "coordinates" : [ -73.99408, 40.75057 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796a6"),
"name" : "10gen Office",
"location" : { "type" : "Point", "coordinates" : [ -73.99171, 40.738868 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796a9"),
"name" : "City Bakery ",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796aa"),
"name" : "Splash Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
}
{
"_id" : ObjectId("5c10f3735d38908db52796ab"),
"name" : "Momofuku Milk Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.985839, 40.731698 ] }
}
Fetching all Documents within a 400 Meter radius from [-73.99171, 40.738868]
would look like this using
GeoJSON:
{
"$geoNear": {
"maxDistance": 400, (1)
"num": 10,
"near": { type: "Point", coordinates: [-73.99171, 40.738868] },
"spherical":true, (2)
"key": "location",
"distanceField": "distance"
}
}
Returning the following 3 Documents:
{
"_id" : ObjectId("5c10f3735d38908db52796a6"),
"name" : "10gen Office",
"location" : { "type" : "Point", "coordinates" : [ -73.99171, 40.738868 ] }
"distance" : 0.0 (3)
}
{
"_id" : ObjectId("5c10f3735d38908db52796a9"),
"name" : "City Bakery ",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 69.3582262492474 (3)
}
{
"_id" : ObjectId("5c10f3735d38908db52796aa"),
"name" : "Splash Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 69.3582262492474 (3)
}
1 | Maximum distance from center point in Meters. |
2 | GeoJSON always operates upon a sphere. |
3 | Distance from center point in Meters. |
Now, when using legacy coordinate pairs one operates upon Radians as discussed before. So we use Metrics#KILOMETERS
when constructing the `$geoNear
command. The Metric
makes sure the distance multiplier is set correctly.
{
"$geoNear": {
"maxDistance": 0.0000627142377, (1)
"distanceMultiplier": 6378.137, (2)
"num": 10,
"near": [-73.99171, 40.738868],
"spherical":true, (3)
"key": "location",
"distanceField": "distance"
}
}
Returning the 3 Documents just like the GeoJSON variant:
{
"_id" : ObjectId("5c10f3735d38908db52796a6"),
"name" : "10gen Office",
"location" : { "type" : "Point", "coordinates" : [ -73.99171, 40.738868 ] }
"distance" : 0.0 (4)
}
{
"_id" : ObjectId("5c10f3735d38908db52796a9"),
"name" : "City Bakery ",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 0.0693586286032982 (4)
}
{
"_id" : ObjectId("5c10f3735d38908db52796aa"),
"name" : "Splash Bar",
"location" : { "type" : "Point", "coordinates" : [ -73.992491, 40.738673 ] }
"distance" : 0.0693586286032982 (4)
}
1 | Maximum distance from center point in Radians. |
2 | The distance multiplier so we get Kilometers as resulting distance. |
3 | Make sure we operate on a 2d_sphere index. |
4 | Distance from center point in Kilometers - take it times 1000 to match Meters of the GeoJSON variant. |
GeoJSON Jackson Modules
By using the Web support, Spring Data registers additional Jackson Modules
s to the ObjectMapper
for de-/serializing common Spring Data domain types.
Please refer to the Spring Data Jackson Modules section to learn more about the infrastructure setup of this feature.
The MongoDB module additionally registers JsonDeserializer
s for the following GeoJSON types via its GeoJsonConfiguration
exposing the GeoJsonModule
.
org.springframework.data.mongodb.core.geo.GeoJsonPoint org.springframework.data.mongodb.core.geo.GeoJsonMultiPoint org.springframework.data.mongodb.core.geo.GeoJsonLineString org.springframework.data.mongodb.core.geo.GeoJsonMultiLineString org.springframework.data.mongodb.core.geo.GeoJsonPolygon org.springframework.data.mongodb.core.geo.GeoJsonMultiPolygon
The
|
The next major version ( |
11.6.6. Full-text Queries
Since version 2.6 of MongoDB, you can run full-text queries by using the $text
operator. Methods and operations specific to full-text queries are available in TextQuery
and TextCriteria
. When doing full text search, see the MongoDB reference for its behavior and limitations.
Full-text Search
Before you can actually use full-text search, you must set up the search index correctly. See Text Index for more detail on how to create index structures. The following example shows how to set up a full-text search:
db.foo.createIndex(
{
title : "text",
content : "text"
},
{
weights : {
title : 3
}
}
)
A query searching for coffee cake
can be defined and run as follows:
Query query = TextQuery
.queryText(new TextCriteria().matchingAny("coffee", "cake"));
List<Document> page = template.find(query, Document.class);
To sort results by relevance according to the weights
use TextQuery.sortByScore
.
Query query = TextQuery
.queryText(new TextCriteria().matchingAny("coffee", "cake"))
.sortByScore() (1)
.includeScore(); (2)
List<Document> page = template.find(query, Document.class);
1 | Use the score property for sorting results by relevance which triggers .sort({'score': {'$meta': 'textScore'}}) . |
2 | Use TextQuery.includeScore() to include the calculated relevance in the resulting Document . |
You can exclude search terms by prefixing the term with -
or by using notMatching
, as shown in the following example (note that the two lines have the same effect and are thus redundant):
// search for 'coffee' and not 'cake'
TextQuery.queryText(new TextCriteria().matching("coffee").matching("-cake"));
TextQuery.queryText(new TextCriteria().matching("coffee").notMatching("cake"));
TextCriteria.matching
takes the provided term as is. Therefore, you can define phrases by putting them between double quotation marks (for example, \"coffee cake\")
or using by TextCriteria.phrase.
The following example shows both ways of defining a phrase:
// search for phrase 'coffee cake'
TextQuery.queryText(new TextCriteria().matching("\"coffee cake\""));
TextQuery.queryText(new TextCriteria().phrase("coffee cake"));
You can set flags for $caseSensitive
and $diacriticSensitive
by using the corresponding methods on TextCriteria
. Note that these two optional flags have been introduced in MongoDB 3.2 and are not included in the query unless explicitly set.
11.6.7. Collations
Since version 3.4, MongoDB supports collations for collection and index creation and various query operations. Collations define string comparison rules based on the ICU collations. A collation document consists of various properties that are encapsulated in Collation
, as the following listing shows:
Collation collation = Collation.of("fr") (1)
.strength(ComparisonLevel.secondary() (2)
.includeCase())
.numericOrderingEnabled() (3)
.alternate(Alternate.shifted().punct()) (4)
.forwardDiacriticSort() (5)
.normalizationEnabled(); (6)
1 | Collation requires a locale for creation. This can be either a string representation of the locale, a Locale (considering language, country, and variant) or a CollationLocale . The locale is mandatory for creation. |
2 | Collation strength defines comparison levels that denote differences between characters. You can configure various options (case-sensitivity, case-ordering, and others), depending on the selected strength. |
3 | Specify whether to compare numeric strings as numbers or as strings. |
4 | Specify whether the collation should consider whitespace and punctuation as base characters for purposes of comparison. |
5 | Specify whether strings with diacritics sort from back of the string, such as with some French dictionary ordering. |
6 | Specify whether to check whether text requires normalization and whether to perform normalization. |
Collations can be used to create collections and indexes. If you create a collection that specifies a collation, the collation is applied to index creation and queries unless you specify a different collation. A collation is valid for a whole operation and cannot be specified on a per-field basis.
Like other metadata, collations can be be derived from the domain type via the collation
attribute of the @Document
annotation and will be applied directly when running queries, creating collections or indexes.
Annotated collations will not be used when a collection is auto created by MongoDB on first interaction. This would
require additional store interaction delaying the entire process. Please use MongoOperations.createCollection for those cases.
|
Collation french = Collation.of("fr");
Collation german = Collation.of("de");
template.createCollection(Person.class, CollectionOptions.just(collation));
template.indexOps(Person.class).ensureIndex(new Index("name", Direction.ASC).collation(german));
MongoDB uses simple binary comparison if no collation is specified (Collation.simple() ).
|
Using collations with collection operations is a matter of specifying a Collation
instance in your query or operation options, as the following two examples show:
find
Collation collation = Collation.of("de");
Query query = new Query(Criteria.where("firstName").is("Amél")).collation(collation);
List<Person> results = template.find(query, Person.class);
aggregate
Collation collation = Collation.of("de");
AggregationOptions options = AggregationOptions.builder().collation(collation).build();
Aggregation aggregation = newAggregation(
project("tags"),
unwind("tags"),
group("tags")
.count().as("count")
).withOptions(options);
AggregationResults<TagCount> results = template.aggregate(aggregation, "tags", TagCount.class);
Indexes are only used if the collation used for the operation matches the index collation. |
MongoDB Repositories support Collations
via the collation
attribute of the @Query
annotation.
public interface PersonRepository extends MongoRepository<Person, String> {
@Query(collation = "en_US") (1)
List<Person> findByFirstname(String firstname);
@Query(collation = "{ 'locale' : 'en_US' }") (2)
List<Person> findPersonByFirstname(String firstname);
@Query(collation = "?1") (3)
List<Person> findByFirstname(String firstname, Object collation);
@Query(collation = "{ 'locale' : '?1' }") (4)
List<Person> findByFirstname(String firstname, String collation);
List<Person> findByFirstname(String firstname, Collation collation); (5)
@Query(collation = "{ 'locale' : 'en_US' }")
List<Person> findByFirstname(String firstname, @Nullable Collation collation); (6)
}
1 | Static collation definition resulting in { 'locale' : 'en_US' } . |
2 | Static collation definition resulting in { 'locale' : 'en_US' } . |
3 | Dynamic collation depending on 2nd method argument. Allowed types include String (eg. 'en_US'), Locacle (eg. Locacle.US)
and Document (eg. new Document("locale", "en_US")) |
4 | Dynamic collation depending on 2nd method argument. |
5 | Apply the Collation method parameter to the query. |
6 | The Collation method parameter overrides the default collation from @Query if not null. |
In case you enabled the automatic index creation for repository finder methods a potential static collation definition, as shown in (1) and (2), will be included when creating the index. |
The most specifc Collation outroules potentially defined others. Which means Method argument over query method annotation over doamin type annotation.
|
JSON Schema
As of version 3.6, MongoDB supports collections that validate documents against a provided JSON Schema. The schema itself and both validation action and level can be defined when creating the collection, as the following example shows:
{
"type": "object", (1)
"required": [ "firstname", "lastname" ], (2)
"properties": { (3)
"firstname": { (4)
"type": "string",
"enum": [ "luke", "han" ]
},
"address": { (5)
"type": "object",
"properties": {
"postCode": { "type": "string", "minLength": 4, "maxLength": 5 }
}
}
}
}
1 | JSON schema documents always describe a whole document from its root. A schema is a schema object itself that can contain embedded schema objects that describe properties and subdocuments. |
2 | required is a property that describes which properties are required in a document. It can be specified optionally, along with other
schema constraints. See MongoDB’s documentation on available keywords. |
3 | properties is related to a schema object that describes an object type. It contains property-specific schema constraints. |
4 | firstname specifies constraints for the firsname field inside the document. Here, it is a string-based properties element declaring
possible field values. |
5 | address is a subdocument defining a schema for values in its postCode field. |
You can provide a schema either by specifying a schema document (that is, by using the Document
API to parse or build a document object) or by building it with Spring Data’s JSON schema utilities in org.springframework.data.mongodb.core.schema
. MongoJsonSchema
is the entry point for all JSON schema-related operations. The following example shows how use MongoJsonSchema.builder()
to create a JSON schema:
MongoJsonSchema.builder() (1)
.required("lastname") (2)
.properties(
required(string("firstname").possibleValues("luke", "han")), (3)
object("address")
.properties(string("postCode").minLength(4).maxLength(5)))
.build(); (4)
1 | Obtain a schema builder to configure the schema with a fluent API. |
2 | Configure required properties either directly as shown here or with more details as in 3. |
3 | Configure the required String-typed firstname field, allowing only luke and han values. Properties can be typed or untyped. Use a static import of JsonSchemaProperty to make the syntax slightly more compact and to get entry points such as string(…) . |
4 | Build the schema object. Use the schema to create either a collection or query documents. |
There are already some predefined and strongly typed schema objects (JsonSchemaObject
and JsonSchemaProperty
) available
through static methods on the gateway interfaces.
However, you may need to build custom property validation rules, which can be created through the builder API, as the following example shows:
// "birthdate" : { "bsonType": "date" }
JsonSchemaProperty.named("birthdate").ofType(Type.dateType());
// "birthdate" : { "bsonType": "date", "description", "Must be a date" }
JsonSchemaProperty.named("birthdate").with(JsonSchemaObject.of(Type.dateType()).description("Must be a date"));
CollectionOptions
provides the entry point to schema support for collections, as the following example shows:
$jsonSchema
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
template.createCollection(Person.class, CollectionOptions.empty().schema(schema));
Generating a Schema
Setting up a schema can be a time consuming task and we encourage everyone who decides to do so, to really take the time it takes.
It’s important, schema changes can be hard.
However, there might be times when one does not want to balked with it, and that is where JsonSchemaCreator
comes into play.
JsonSchemaCreator
and its default implementation generates a MongoJsonSchema
out of domain types metadata provided by the mapping infrastructure.
This means, that annotated properties as well as potential custom conversions are considered.
public class Person {
private final String firstname; (1)
private final int age; (2)
private Species species; (3)
private Address address; (4)
private @Field(fieldType=SCRIPT) String theForce; (5)
private @Transient Boolean useTheForce; (6)
public Person(String firstname, int age) { (1) (2)
this.firstname = firstname;
this.age = age;
}
// gettter / setter omitted
}
MongoJsonSchema schema = MongoJsonSchemaCreator.create(mongoOperations.getConverter())
.createSchemaFor(Person.class);
template.createCollection(Person.class, CollectionOptions.empty().schema(schema));
{
'type' : 'object',
'required' : ['age'], (2)
'properties' : {
'firstname' : { 'type' : 'string' }, (1)
'age' : { 'bsonType' : 'int' } (2)
'species' : { (3)
'type' : 'string',
'enum' : ['HUMAN', 'WOOKIE', 'UNKNOWN']
}
'address' : { (4)
'type' : 'object'
'properties' : {
'postCode' : { 'type': 'string' }
}
},
'theForce' : { 'type' : 'javascript'} (5)
}
}
1 | Simple object properties are consideres regular properties. |
2 | Primitive types are considered required properties |
3 | Enums are restricted to possible values. |
4 | Object type properties are inspected and represented as nested documents. |
5 | String type property that is converted to Code by the converter. |
6 | @Transient properties are omitted when generating the schema. |
_id properties using types that can be converted into ObjectId like String are mapped to { type : 'object' }
unless there is more specific information available via the @MongoId annotation.
|
Java | Schema Type | Notes |
---|---|---|
|
|
with |
|
|
- |
|
|
- |
|
|
with |
|
|
simple type array unless it’s a |
|
|
- |
Query a collection for matching JSON Schema
You can use a schema to query any collection for documents that match a given structure defined by a JSON schema, as the following example shows:
$jsonSchema
MongoJsonSchema schema = MongoJsonSchema.builder().required("firstname", "lastname").build();
template.find(query(matchingDocumentStructure(schema)), Person.class);
Encrypted Fields
MongoDB 4.2 Field Level Encryption allows to directly encrypt individual properties.
Properties can be wrapped within an encrypted property when setting up the JSON Schema as shown in the example below.
MongoJsonSchema schema = MongoJsonSchema.builder()
.properties(
encrypted(string("ssn"))
.algorithm("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
.keyId("*key0_id")
).build();
Instead of defining encrypted fields manually it is possible leverage the @Encrypted
annotation as shown in the snippet below.
@Document
@Encrypted(keyId = "xKVup8B1Q+CkHaVRx+qa+g==", algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Random") (1)
static class Patient {
@Id String id;
String name;
@Encrypted (2)
String bloodType;
@Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic") (3)
Integer ssn;
}
1 | Default encryption settings that will be set for encryptMetadata . |
2 | Encrypted field using default encryption settings. |
3 | Encrypted field overriding the default encryption algorithm. |
The
The
To combine derived encryption settings with
|
Make sure to set the drivers com.mongodb.AutoEncryptionSettings to use client-side encryption. MongoDB does not support encryption for all field types. Specific data types require deterministic encryption to preserve equality comparison functionality.
|
JSON Schema Types
The following table shows the supported JSON schema types:
Schema Type | Java Type | Schema Properties |
---|---|---|
|
- |
|
|
|
|
|
any array except |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(none) |
|
|
(none) |
|
|
(none) |
|
|
(none) |
|
|
(none) |
|
|
(none) |
|
|
(none) |
untyped is a generic type that is inherited by all typed schema types. It provides all untyped schema properties to typed schema types.
|
For more information, see $jsonSchema.
11.6.8. Fluent Template API
The MongoOperations
interface is one of the central components when it comes to more low-level interaction with MongoDB. It offers a wide range of methods covering needs from collection creation, index creation, and CRUD operations to more advanced functionality, such as Map-Reduce and aggregations.
You can find multiple overloads for each method. Most of them cover optional or nullable parts of the API.
FluentMongoOperations
provides a more narrow interface for the common methods of MongoOperations
and provides a more readable, fluent API.
The entry points (insert(…)
, find(…)
, update(…)
, and others) follow a natural naming schema based on the operation to be run. Moving on from the entry point, the API is designed to offer only context-dependent methods that lead to a terminating method that invokes the actual MongoOperations
counterpart — the all
method in the case of the following example:
List<SWCharacter> all = ops.find(SWCharacter.class)
.inCollection("star-wars") (1)
.all();
1 | Skip this step if SWCharacter defines the collection with @Document or if you use the class name as the collection name, which is fine. |
Sometimes, a collection in MongoDB holds entities of different types, such as a Jedi
within a collection of SWCharacters
.
To use different types for Query
and return value mapping, you can use as(Class<?> targetType)
to map results differently, as the following example shows:
List<Jedi> all = ops.find(SWCharacter.class) (1)
.as(Jedi.class) (2)
.matching(query(where("jedi").is(true)))
.all();
1 | The query fields are mapped against the SWCharacter type. |
2 | Resulting documents are mapped into Jedi . |
You can directly apply Projections to result documents by providing the target type via as(Class<?>) .
|
Using projections allows MongoTemplate to optimize result mapping by limiting the actual response to fields required
by the projection target type. This applies as long as the Query itself does not contain any field restriction and the
target type is a closed interface or DTO projection.
|
You can switch between retrieving a single entity and retrieving multiple entities as a List
or a Stream
through the terminating methods: first()
, one()
, all()
, or stream()
.
When writing a geo-spatial query with near(NearQuery)
, the number of terminating methods is altered to include only the methods that are valid for running a geoNear
command in MongoDB (fetching entities as a GeoResult
within GeoResults
), as the following example shows:
GeoResults<Jedi> results = mongoOps.query(SWCharacter.class)
.as(Jedi.class)
.near(alderaan) // NearQuery.near(-73.9667, 40.78).maxDis…
.all();
11.6.9. Type-safe Queries for Kotlin
Kotlin embraces domain-specific language creation through its language syntax and its extension system.
Spring Data MongoDB ships with a Kotlin Extension for Criteria
using Kotlin property references to build type-safe queries.
Queries using this extension are typically benefit from improved readability.
Most keywords on Criteria
have a matching Kotlin extension, such as inValues
and regex
.
Consider the following example explaining Type-safe Queries:
import org.springframework.data.mongodb.core.query.*
mongoOperations.find<Book>(
Query(Book::title isEqualTo "Moby-Dick") (1)
)
mongoOperations.find<Book>(
Query(titlePredicate = Book::title exists true)
)
mongoOperations.find<Book>(
Query(
Criteria().andOperator(
Book::price gt 5,
Book::price lt 10
))
)
// Binary operators
mongoOperations.find<BinaryMessage>(
Query(BinaryMessage::payload bits { allClear(0b101) }) (2)
)
// Nested Properties (i.e. refer to "book.author")
mongoOperations.find<Book>(
Query(Book::author / Author::name regex "^H") (3)
)
1 | isEqualTo() is an infix extension function with receiver type KProperty<T> that returns Criteria . |
2 | For bitwise operators, pass a lambda argument where you call one of the methods of Criteria.BitwiseCriteriaOperators . |
3 | To construct nested properties, use the / character (overloaded operator div ). |
11.6.10. Additional Query Options
MongoDB offers various ways of applying meta information, like a comment or a batch size, to a query.Using the Query
API
directly there are several methods for those options.
Query query = query(where("firstname").is("luke"))
.comment("find luke") (1)
.batchSize(100) (2)
1 | The comment propagated to the MongoDB profile log. |
2 | The number of documents to return in each response batch. |
On the repository level the @Meta
annotation provides means to add query options in a declarative way.
@Meta(comment = "find luke", batchSize = 100, flags = { SLAVE_OK })
List<Person> findByFirstname(String firstname);
11.7. Query by Example
11.7.1. Introduction
This chapter provides an introduction to Query by Example and explains how to use it.
Query by Example (QBE) is a user-friendly querying technique with a simple interface. It allows dynamic query creation and does not require you to write queries that contain field names. In fact, Query by Example does not require you to write queries by using store-specific query languages at all.
11.7.2. Usage
The Query by Example API consists of three parts:
-
Probe: The actual example of a domain object with populated fields.
-
ExampleMatcher
: TheExampleMatcher
carries details on how to match particular fields. It can be reused across multiple Examples. -
Example
: AnExample
consists of the probe and theExampleMatcher
. It is used to create the query.
Query by Example is well suited for several use cases:
-
Querying your data store with a set of static or dynamic constraints.
-
Frequent refactoring of the domain objects without worrying about breaking existing queries.
-
Working independently from the underlying data store API.
Query by Example also has several limitations:
-
No support for nested or grouped property constraints, such as
firstname = ?0 or (firstname = ?1 and lastname = ?2)
. -
Only supports starts/contains/ends/regex matching for strings and exact matching for other property types.
Before getting started with Query by Example, you need to have a domain object. To get started, create an interface for your repository, as shown in the following example:
public class Person {
@Id
private String id;
private String firstname;
private String lastname;
private Address address;
// … getters and setters omitted
}
The preceding example shows a simple domain object.
You can use it to create an Example
.
By default, fields having null
values are ignored, and strings are matched by using the store specific defaults.
Inclusion of properties into a Query by Example criteria is based on nullability. Properties using primitive types (int , double , …) are always included unless ignoring the property path.
|
Examples can be built by either using the of
factory method or by using ExampleMatcher
. Example
is immutable.
The following listing shows a simple Example:
Person person = new Person(); (1)
person.setFirstname("Dave"); (2)
Example<Person> example = Example.of(person); (3)
1 | Create a new instance of the domain object. |
2 | Set the properties to query. |
3 | Create the Example . |
You can run the example queries by using repositories.
To do so, let your repository interface extend QueryByExampleExecutor<T>
.
The following listing shows an excerpt from the QueryByExampleExecutor
interface:
QueryByExampleExecutor
public interface QueryByExampleExecutor<T> {
<S extends T> S findOne(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example);
// … more functionality omitted.
}
11.7.3. Example Matchers
Examples are not limited to default settings.
You can specify your own defaults for string matching, null handling, and property-specific settings by using the ExampleMatcher
, as shown in the following example:
Person person = new Person(); (1)
person.setFirstname("Dave"); (2)
ExampleMatcher matcher = ExampleMatcher.matching() (3)
.withIgnorePaths("lastname") (4)
.withIncludeNullValues() (5)
.withStringMatcher(StringMatcher.ENDING); (6)
Example<Person> example = Example.of(person, matcher); (7)
1 | Create a new instance of the domain object. |
2 | Set properties. |
3 | Create an ExampleMatcher to expect all values to match.
It is usable at this stage even without further configuration. |
4 | Construct a new ExampleMatcher to ignore the lastname property path. |
5 | Construct a new ExampleMatcher to ignore the lastname property path and to include null values. |
6 | Construct a new ExampleMatcher to ignore the lastname property path, to include null values, and to perform suffix string matching. |
7 | Create a new Example based on the domain object and the configured ExampleMatcher . |
By default, the ExampleMatcher
expects all values set on the probe to match.
If you want to get results matching any of the predicates defined implicitly, use ExampleMatcher.matchingAny()
.
You can specify behavior for individual properties (such as "firstname" and "lastname" or, for nested properties, "address.city"). You can tune it with matching options and case sensitivity, as shown in the following example:
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstname", endsWith())
.withMatcher("lastname", startsWith().ignoreCase());
}
Another way to configure matcher options is to use lambdas (introduced in Java 8). This approach creates a callback that asks the implementor to modify the matcher. You need not return the matcher, because configuration options are held within the matcher instance. The following example shows a matcher that uses lambdas:
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstname", match -> match.endsWith())
.withMatcher("firstname", match -> match.startsWith());
}
Queries created by Example
use a merged view of the configuration.
Default matching settings can be set at the ExampleMatcher
level, while individual settings can be applied to particular property paths.
Settings that are set on ExampleMatcher
are inherited by property path settings unless they are defined explicitly.
Settings on a property patch have higher precedence than default settings.
The following table describes the scope of the various ExampleMatcher
settings:
Setting | Scope |
---|---|
Null-handling |
|
String matching |
|
Ignoring properties |
Property path |
Case sensitivity |
|
Value transformation |
Property path |
11.7.4. Running an Example
The following example shows how to query by example when using a repository (of Person
objects, in this case):
public interface PersonRepository extends QueryByExampleExecutor<Person> {
}
public class PersonService {
@Autowired PersonRepository personRepository;
public List<Person> findPeople(Person probe) {
return personRepository.findAll(Example.of(probe));
}
}
An Example
containing an untyped ExampleSpec
uses the Repository type and its collection name. Typed ExampleSpec
instances use their type as the result type and the collection name from the Repository
instance.
When including null values in the ExampleSpec , Spring Data Mongo uses embedded document matching instead of dot notation property matching. Doing so forces exact document matching for all property values and the property order in the embedded document.
|
Spring Data MongoDB provides support for the following matching options:
Matching | Logical result |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11.7.5. Untyped Example
By default Example
is strictly typed. This means that the mapped query has an included type match, restricting it to probe assignable types. For example, when sticking with the default type key (_class
), the query has restrictions such as (_class : { $in : [ com.acme.Person] }
).
By using the UntypedExampleMatcher
, it is possible to bypass the default behavior and skip the type restriction. So, as long as field names match, nearly any domain type can be used as the probe for creating the reference, as the following example shows:
class JustAnArbitraryClassWithMatchingFieldName {
@Field("lastname") String value;
}
JustAnArbitraryClassWithMatchingFieldNames probe = new JustAnArbitraryClassWithMatchingFieldNames();
probe.value = "stark";
Example example = Example.of(probe, UntypedExampleMatcher.matching());
Query query = new Query(new Criteria().alike(example));
List<Person> result = template.find(query, Person.class);
Also, keep in mind that using |
11.8. Counting Documents
In pre-3.x versions of SpringData MongoDB the count operation used MongoDBs internal collection statistics.
With the introduction of MongoDB Transactions this was no longer possible because statistics would not correctly reflect potential changes during a transaction requiring an aggregation-based count approach.
So in version 2.x MongoOperations.count()
would use the collection statistics if no transaction was in progress, and the aggregation variant if so.
As of Spring Data MongoDB 3.x any count
operation uses regardless the existence of filter criteria the aggregation-based count approach via MongoDBs countDocuments
.
If the application is fine with the limitations of working upon collection statistics MongoOperations.estimatedCount()
offers an alternative.
MongoDBs native Therefore a given
|
11.9. Map-Reduce Operations
You can query MongoDB by using Map-Reduce, which is useful for batch processing, for data aggregation, and for when the query language does not fulfill your needs.
Spring provides integration with MongoDB’s Map-Reduce by providing methods on MongoOperations
to simplify the creation and running of Map-Reduce operations.It can convert the results of a Map-Reduce operation to a POJO and integrates with Spring’s Resource abstraction.This lets you place your JavaScript files on the file system, classpath, HTTP server, or any other Spring Resource implementation and then reference the JavaScript resources through an easy URI style syntax — for example, 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.
11.9.1. Example Usage
To understand how to perform Map-Reduce operations, we use an example from the book, MongoDB - The Definitive Guide [1].In this example, we create three documents that have the values [a,b], [b,c], and [c,d], respectively.The values in each document are associated with the key, 'x', as the following example shows (assume these documents are in a collection named jmr1
):
{ "_id" : ObjectId("4e5ff893c0277826074ec533"), "x" : [ "a", "b" ] }
{ "_id" : ObjectId("4e5ff893c0277826074ec534"), "x" : [ "b", "c" ] }
{ "_id" : ObjectId("4e5ff893c0277826074ec535"), "x" : [ "c", "d" ] }
The following map function counts the occurrence of each letter in the array for each document:
function () {
for (var i = 0; i < this.x.length; i++) {
emit(this.x[i], 1);
}
}
The follwing reduce function sums up the occurrence of each letter across all the documents:
function (key, values) {
var sum = 0;
for (var i = 0; i < values.length; i++)
sum += values[i];
return sum;
}
Running the preceding functions result in the following collection:
{ "_id" : "a", "value" : 1 }
{ "_id" : "b", "value" : 2 }
{ "_id" : "c", "value" : 2 }
{ "_id" : "d", "value" : 1 }
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 run a Map-Reduce operation as follows:
MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js", ValueObject.class);
for (ValueObject valueObject : results) {
System.out.println(valueObject);
}
The preceding exmaple produces the following output:
ValueObject [id=a, value=1.0]
ValueObject [id=b, value=2.0]
ValueObject [id=c, value=2.0]
ValueObject [id=d, value=1.0]
The MapReduceResults
class implements Iterable
and provides access to the raw output and timing and count statistics.The following listing shows the ValueObject
class:
public class ValueObject {
private String id;
private float value;
public String getId() {
return id;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
@Override
public String toString() {
return "ValueObject [id=" + id + ", value=" + value + "]";
}
}
By default, the output type of INLINE
is used so that you need not 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 compact syntax.The following example 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);
There is also a static import (import static org.springframework.data.mongodb.core.mapreduce.MapReduceOptions.options;
) that can be used to make the syntax slightly more compact, as the following example shows:
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 is fed into the Map-Reduce operation.The following example removes the document that contains [a,b] from consideration for Map-Reduce operations:
Query query = new Query(where("x").ne(new String[] { "a", "b" }));
MapReduceResults<ValueObject> results = mongoOperations.mapReduce(query, "jmr1", "classpath:map.js", "classpath:reduce.js",
options().outputCollection("jmr1_out"), ValueObject.class);
Note that you can specify additional limit and sort values on the query, but you cannot skip values.
11.10. Script Operations
MongoDB 4.2 removed support for the |
MongoDB allows running JavaScript functions on the server by either directly sending the script or calling a stored one. ScriptOperations
can be accessed through MongoTemplate
and provides basic abstraction for JavaScript
usage. The following example shows how to us the ScriptOperations
class:
ScriptOperations scriptOps = template.scriptOps();
ExecutableMongoScript echoScript = new ExecutableMongoScript("function(x) { return x; }");
scriptOps.execute(echoScript, "directly execute script"); (1)
scriptOps.register(new NamedMongoScript("echo", echoScript)); (2)
scriptOps.call("echo", "execute script via name"); (3)
1 | Run the script directly without storing the function on server side. |
2 | Store the script using 'echo' as its name. The given name identifies the script and allows calling it later. |
3 | Run the script with name 'echo' using the provided parameters. |
11.11. Group Operations
As an alternative to using Map-Reduce to perform data aggregation, you can use the group
operation which feels similar to using SQL’s group by query style, so it may feel more approachable vs. using Map-Reduce. Using the group operations does have some limitations, for example it is not supported in a shared 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 running 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.
11.11.1. 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.
{ "_id" : ObjectId("4ec1d25d41421e2015da64f1"), "x" : 1 }
{ "_id" : ObjectId("4ec1d25d41421e2015da64f2"), "x" : 1 }
{ "_id" : ObjectId("4ec1d25d41421e2015da64f3"), "x" : 2 }
{ "_id" : ObjectId("4ec1d25d41421e2015da64f4"), "x" : 3 }
{ "_id" : ObjectId("4ec1d25d41421e2015da64f5"), "x" : 3 }
{ "_id" : ObjectId("4ec1d25d41421e2015da64f6"), "x" : 3 }
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 run the group operation is shown below
GroupByResults<XObject> results = mongoTemplate.group("group_test_collection",
GroupBy.key("x").initialDocument("{ count: 0 }").reduceFunction("function(doc, prev) { prev.count += 1 }"),
XObject.class);
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.
public class XObject {
private float x;
private float count;
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getCount() {
return count;
}
public void setCount(float count) {
this.count = count;
}
@Override
public String toString() {
return "XObject [x=" + x + " count = " + count + "]";
}
}
You can also obtain the raw result as a Document
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.
import static org.springframework.data.mongodb.core.mapreduce.GroupBy.keyFunction;
import static org.springframework.data.mongodb.core.query.Criteria.where;
GroupByResults<XObject> results = mongoTemplate.group(where("x").gt(0),
"group_test_collection",
keyFunction("classpath:keyFunction.js").initialDocument("{ count: 0 }").reduceFunction("classpath:groupReduce.js"), XObject.class);
11.12. Aggregation Framework Support
Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2.
For further information, see the full reference documentation of the aggregation framework and other data aggregation tools for MongoDB.
11.12.1. Basic Concepts
The Aggregation Framework support in Spring Data MongoDB is based on the following key abstractions: Aggregation
, AggregationDefinition
, and AggregationResults
.
-
Aggregation
An
Aggregation
represents a MongoDBaggregate
operation and holds the description of the aggregation pipeline instructions. Aggregations are created by invoking the appropriatenewAggregation(…)
static factory method of theAggregation
class, which takes a list ofAggregateOperation
and an optional input class.The actual aggregate operation is run by the
aggregate
method of theMongoTemplate
, which takes the desired output class as a parameter. -
TypedAggregation
A
TypedAggregation
, just like anAggregation
, holds the instructions of the aggregation pipeline and a reference to the input type, that is used for mapping domain properties to actual document fields.At runtime, field references get checked against the given input type, considering potential
@Field
annotations.
Changed in 3.2 referencing non-existent properties does no longer raise errors. To restore the previous behaviour use the strictMapping
option of AggregationOptions
.
-
AggregationDefinition
An
AggregationDefinition
represents a MongoDB aggregation pipeline operation and describes the processing that should be performed in this aggregation step. Although you could manually create anAggregationDefinition
, we recommend using the static factory methods provided by theAggregate
class to construct anAggregateOperation
. -
AggregationResults
AggregationResults
is the container for the result of an aggregate operation. It provides access to the raw aggregation result, in the form of aDocument
to the mapped objects and other information about the aggregation.The following listing shows the canonical example for using the Spring Data MongoDB support for the MongoDB Aggregation Framework:
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; Aggregation agg = newAggregation( pipelineOP1(), pipelineOP2(), pipelineOPn() ); 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
derives the name of the input collection from this class. Otherwise, if you do not not specify an input class, you must provide the name of the input collection explicitly. If both an input class and an input collection are provided, the latter takes precedence.
11.12.2. Supported Aggregation Operations
The MongoDB Aggregation Framework provides the following types of aggregation operations:
-
Pipeline Aggregation Operators
-
Group/Accumulator Aggregation Operators
-
Boolean Aggregation Operators
-
Comparison Aggregation Operators
-
Arithmetic Aggregation Operators
-
String Aggregation Operators
-
Date Aggregation Operators
-
Array Aggregation Operators
-
Conditional Aggregation Operators
-
Lookup Aggregation Operators
-
Convert Aggregation Operators
-
Object Aggregation Operators
-
Script Aggregation Operators
At the time of this writing, we provide support for the following Aggregation Operations in Spring Data MongoDB:
Pipeline Aggregation Operators |
|
Set Aggregation Operators |
|
Group/Accumulator Aggregation Operators |
|
Arithmetic Aggregation Operators |
|
String Aggregation Operators |
|
Comparison Aggregation Operators |
|
Array Aggregation Operators |
|
Literal Operators |
|
Date Aggregation Operators |
|
Variable Operators |
|
Conditional Aggregation Operators |
|
Type Aggregation Operators |
|
Convert Aggregation Operators |
|
Object Aggregation Operators |
|
Script Aggregation Operators |
|
* The operation is mapped or added by Spring Data MongoDB.
Note that the aggregation operations not listed here are currently not supported by Spring Data MongoDB. Comparison aggregation operators are expressed as Criteria
expressions.
11.12.3. Projection Expressions
Projection expressions are used to define the fields that are the outcome of a particular aggregation step. Projection expressions can be defined through the project
method of the Aggregation
class, either by passing a list of String
objects or an aggregation framework Fields
object. The projection can be extended with additional fields through a fluent API by using the and(String)
method and aliased by using the as(String)
method.
Note that you can also define fields with aliases by using the Fields.field
static factory method of the aggregation framework, which you can then use to construct a new Fields
instance. References to projected fields in later aggregation stages are valid only for the field names of included fields or their aliases (including newly defined fields and their aliases). Fields not included in the projection cannot be referenced in later aggregation stages. The following listings show examples of projection expression:
// generates {$project: {name: 1, netPrice: 1}}
project("name", "netPrice")
// generates {$project: {thing1: $thing2}}
project().and("thing1").as("thing2")
// generates {$project: {a: 1, b: 1, thing2: $thing1}}
project("a","b").and("thing1").as("thing2")
// generates {$project: {name: 1, netPrice: 1}}, {$sort: {name: 1}}
project("name", "netPrice"), sort(ASC, "name")
// generates {$project: {name: $firstname}}, {$sort: {name: 1}}
project().and("firstname").as("name"), sort(ASC, "name")
// does not work
project().and("firstname").as("name"), sort(ASC, "firstname")
More examples for project operations can be found in the AggregationTests
class. Note that further details regarding the projection expressions can be found in the corresponding section of the MongoDB Aggregation Framework reference documentation.
11.12.4. Faceted Classification
As of Version 3.4, MongoDB supports faceted classification by using the Aggregation Framework. A faceted classification uses semantic categories (either general or subject-specific) that are combined to create the full classification entry. Documents flowing through the aggregation pipeline are classified into buckets. A multi-faceted classification enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.
Buckets
Bucket operations categorize incoming documents into groups, called buckets, based on a specified expression and bucket boundaries. Bucket operations require a grouping field or a grouping expression. You can define them by using the bucket()
and bucketAuto()
methods of the Aggregate
class. BucketOperation
and BucketAutoOperation
can expose accumulations based on aggregation expressions for input documents. You can extend the bucket operation with additional parameters through a fluent API by using the with…()
methods and the andOutput(String)
method. You can alias the operation by using the as(String)
method. Each bucket is represented as a document in the output.
BucketOperation
takes a defined set of boundaries to group incoming documents into these categories. Boundaries are required to be sorted. The following listing shows some examples of bucket operations:
// generates {$bucket: {groupBy: $price, boundaries: [0, 100, 400]}}
bucket("price").withBoundaries(0, 100, 400);
// generates {$bucket: {groupBy: $price, default: "Other" boundaries: [0, 100]}}
bucket("price").withBoundaries(0, 100).withDefault("Other");
// generates {$bucket: {groupBy: $price, boundaries: [0, 100], output: { count: { $sum: 1}}}}
bucket("price").withBoundaries(0, 100).andOutputCount().as("count");
// generates {$bucket: {groupBy: $price, boundaries: [0, 100], 5, output: { titles: { $push: "$title"}}}
bucket("price").withBoundaries(0, 100).andOutput("title").push().as("titles");
BucketAutoOperation
determines boundaries in an attempt to evenly distribute documents into a specified number of buckets. BucketAutoOperation
optionally takes a granularity value that specifies the preferred number series to use to ensure that the calculated boundary edges end on preferred round numbers or on powers of 10. The following listing shows examples of bucket operations:
// generates {$bucketAuto: {groupBy: $price, buckets: 5}}
bucketAuto("price", 5)
// generates {$bucketAuto: {groupBy: $price, buckets: 5, granularity: "E24"}}
bucketAuto("price", 5).withGranularity(Granularities.E24).withDefault("Other");
// generates {$bucketAuto: {groupBy: $price, buckets: 5, output: { titles: { $push: "$title"}}}
bucketAuto("price", 5).andOutput("title").push().as("titles");
To create output fields in buckets, bucket operations can use AggregationExpression
through andOutput()
and SpEL expressions through andOutputExpression()
.
Note that further details regarding bucket expressions can be found in the $bucket
section and
$bucketAuto
section of the MongoDB Aggregation Framework reference documentation.
Multi-faceted Aggregation
Multiple aggregation pipelines can be used to create multi-faceted aggregations that characterize data across multiple dimensions (or facets) within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. A common implementation of faceting is how many online retailers provide ways to narrow down search results by applying filters on product price, manufacturer, size, and other factors.
You can define a FacetOperation
by using the facet()
method of the Aggregation
class. You can customize it with multiple aggregation pipelines by using the and()
method. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.
Sub-pipelines can project and filter input documents prior to grouping. Common use cases include extraction of date parts or calculations before categorization. The following listing shows facet operation examples:
// generates {$facet: {categorizedByPrice: [ { $match: { price: {$exists : true}}}, { $bucketAuto: {groupBy: $price, buckets: 5}}]}}
facet(match(Criteria.where("price").exists(true)), bucketAuto("price", 5)).as("categorizedByPrice"))
// generates {$facet: {categorizedByCountry: [ { $match: { country: {$exists : true}}}, { $sortByCount: "$country"}]}}
facet(match(Criteria.where("country").exists(true)), sortByCount("country")).as("categorizedByCountry"))
// generates {$facet: {categorizedByYear: [
// { $project: { title: 1, publicationYear: { $year: "publicationDate"}}},
// { $bucketAuto: {groupBy: $price, buckets: 5, output: { titles: {$push:"$title"}}}
// ]}}
facet(project("title").and("publicationDate").extractYear().as("publicationYear"),
bucketAuto("publicationYear", 5).andOutput("title").push().as("titles"))
.as("categorizedByYear"))
Note that further details regarding facet operation can be found in the $facet
section of the MongoDB Aggregation Framework reference documentation.
Sort By Count
Sort by count operations group incoming documents based on the value of a specified expression, compute the count of documents in each distinct group, and sort the results by count. It offers a handy shortcut to apply sorting when using Faceted Classification. Sort by count operations require a grouping field or grouping expression. The following listing shows a sort by count example:
// generates { $sortByCount: "$country" }
sortByCount("country");
A sort by count operation is equivalent to the following BSON (Binary JSON):
{ $group: { _id: <expression>, count: { $sum: 1 } } }, { $sort: { count: -1 } }
Spring Expression Support in Projection Expressions
We support the use of SpEL expressions in projection expressions through the andExpression
method of the ProjectionOperation
and BucketOperation
classes. This feature lets you define the desired expression as a SpEL expression. On running a query, the SpEL expression is translated into a corresponding MongoDB projection expression part. This arrangement makes it much easier to express complex calculations.
Complex Calculations with SpEL expressions
Consider the following SpEL expression:
1 + (q + 1) / (q - 1)
The preceding expression is translated into the following projection expression part:
{ "$add" : [ 1, {
"$divide" : [ {
"$add":["$q", 1]}, {
"$subtract":[ "$q", 1]}
]
}]}
You can see examples in more context in Aggregation Framework Example 5 and Aggregation Framework Example 6. You can find more usage examples for supported SpEL expression constructs in SpelExpressionTransformerUnitTests
. The following table shows the SpEL transformations supported by Spring Data MongoDB:
SpEL Expression | Mongo Expression Part |
---|---|
a == b |
{ $eq : [$a, $b] } |
a != b |
{ $ne : [$a , $b] } |
a > b |
{ $gt : [$a, $b] } |
a >= b |
{ $gte : [$a, $b] } |
a < b |
{ $lt : [$a, $b] } |
a ⇐ b |
{ $lte : [$a, $b] } |
a + b |
{ $add : [$a, $b] } |
a - b |
{ $subtract : [$a, $b] } |
a * b |
{ $multiply : [$a, $b] } |
a / b |
{ $divide : [$a, $b] } |
a^b |
{ $pow : [$a, $b] } |
a % b |
{ $mod : [$a, $b] } |
a && b |
{ $and : [$a, $b] } |
a || b |
{ $or : [$a, $b] } |
!a |
{ $not : [$a] } |
In addition to the transformations shown in the preceding table, you can use standard SpEL operations such as new
to (for example) create arrays and reference expressions through their names (followed by the arguments to use in brackets). The following example shows how to create an array in this fashion:
// { $setEquals : [$a, [5, 8, 13] ] }
.andExpression("setEquals(a, new int[]{5, 8, 13})");
Aggregation Framework Examples
The examples in this section demonstrate the usage patterns for the MongoDB Aggregation Framework with Spring Data MongoDB.
Aggregation Framework Example 1
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;
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
Aggregation agg = newAggregation(
project("tags"),
unwind("tags"),
group("tags").count().as("n"),
project("n").and("tag").previousOperation(),
sort(DESC, "n")
);
AggregationResults<TagCount> results = mongoTemplate.aggregate(agg, "tags", TagCount.class);
List<TagCount> tagCount = results.getMappedResults();
The preceding listing uses the following algorithm:
-
Create a new aggregation by using the
newAggregation
static factory method, to which we pass a list of aggregation operations. These aggregate operations define the aggregation pipeline of ourAggregation
. -
Use the
project
operation to select thetags
field (which is an array of strings) from the input collection. -
Use the
unwind
operation to generate a new document for each tag within thetags
array. -
Use the
group
operation to define a group for eachtags
value for which we aggregate the occurrence count (by using thecount
aggregation operator and collecting the result in a new field calledn
). -
Select the
n
field and create an alias for the ID field generated from the previous group operation (hence the call topreviousOperation()
) with a name oftag
. -
Use the
sort
operation to sort the resulting list of tags by their occurrence count in descending order. -
Call the
aggregate
method onMongoTemplate
to let MongoDB perform the actual aggregation operation, with the createdAggregation
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 the first parameter to the newAggreation
method.
Aggregation Framework Example 2
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 by using the aggregation framework. This example demonstrates 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;
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
TypedAggregation<ZipInfo> aggregation = newAggregation(ZipInfo.class,
group("state", "city")
.sum("population").as("pop"),
sort(ASC, "pop", "state", "city"),
group("state")
.last("city").as("biggestCity")
.last("pop").as("biggestPop")
.first("city").as("smallestCity")
.first("pop").as("smallestPop"),
project()
.and("state").previousOperation()
.and("biggestCity")
.nested(bind("name", "biggestCity").and("population", "biggestPop"))
.and("smallestCity")
.nested(bind("name", "smallestCity").and("population", "smallestPop")),
sort(ASC, "state")
);
AggregationResults<ZipInfoStats> result = mongoTemplate.aggregate(aggregation, ZipInfoStats.class);
ZipInfoStats firstZipInfoStats = result.getMappedResults().get(0);
Note that the ZipInfo
class maps the structure of the given input-collection. The ZipInfoStats
class defines the structure in the desired output format.
The preceding listings use the following algorithm:
-
Use the
group
operation to define a group from the input-collection. The grouping criteria is the combination of thestate
andcity
fields, which forms the ID structure of the group. We aggregate the value of thepopulation
property from the grouped elements by using thesum
operator and save the result in thepop
field. -
Use the
sort
operation to sort the intermediate-result by thepop
,state
andcity
fields, 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 onstate
andcity
is implicitly performed against the group ID fields (which Spring Data MongoDB handled). -
Use a
group
operation again to group the intermediate result bystate
. Note thatstate
again implicitly references a group ID field. We select the name and the population count of the biggest and smallest city with calls to thelast(…)
andfirst(…)
operators, respectively, in theproject
operation. -
Select the
state
field from the previousgroup
operation. Note thatstate
again implicitly references a group ID field. Because we do not want an implicitly generated ID to appear, we exclude the ID from the previous operation by usingand(previousOperation()).exclude()
. Because we want to populate the nestedCity
structures in our output class, we have to emit appropriate sub-documents by using the nested method. -
Sort the resulting list of
StateStats
by their state name in ascending order in thesort
operation.
Note that we derive the name of the input collection from the ZipInfo
class passed as the first parameter to the newAggregation
method.
Aggregation Framework Example 3
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 grouping, sorting, and matching (filtering).
class StateStats {
@Id String id;
String state;
@Field("totalPop") int totalPopulation;
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
TypedAggregation<ZipInfo> agg = newAggregation(ZipInfo.class,
group("state").sum("population").as("totalPop"),
sort(ASC, previousOperation(), "totalPop"),
match(where("totalPop").gte(10 * 1000 * 1000))
);
AggregationResults<StateStats> result = mongoTemplate.aggregate(agg, StateStats.class);
List<StateStats> stateStatsList = result.getMappedResults();
The preceding listings use the following algorithm:
-
Group the input collection by the
state
field and calculate the sum of thepopulation
field and store the result in the new field"totalPop"
. -
Sort the intermediate result by the id-reference of the previous group operation in addition to the
"totalPop"
field in ascending order. -
Filter the intermediate result by using a
match
operation which accepts aCriteria
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.
Aggregation Framework Example 4
This example demonstrates the use of simple arithmetic operations in the projection operation.
class Product {
String id;
String name;
double netPrice;
int spaceUnits;
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
TypedAggregation<Product> agg = newAggregation(Product.class,
project("name", "netPrice")
.and("netPrice").plus(1).as("netPricePlus1")
.and("netPrice").minus(1).as("netPriceMinus1")
.and("netPrice").multiply(1.19).as("grossPrice")
.and("netPrice").divide(2).as("netPriceDiv2")
.and("spaceUnits").mod(2).as("spaceUnitsMod2")
);
AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);
List<Document> resultList = result.getMappedResults();
Note that we derive the name of the input collection from the Product
class passed as first parameter to the newAggregation
method.
Aggregation Framework Example 5
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;
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
TypedAggregation<Product> agg = newAggregation(Product.class,
project("name", "netPrice")
.andExpression("netPrice + 1").as("netPricePlus1")
.andExpression("netPrice - 1").as("netPriceMinus1")
.andExpression("netPrice / 2").as("netPriceDiv2")
.andExpression("netPrice * 1.19").as("grossPrice")
.andExpression("spaceUnits % 2").as("spaceUnitsMod2")
.andExpression("(netPrice * 0.8 + 1.2) * 1.19").as("grossPriceIncludingDiscountAndCharge")
);
AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);
List<Document> resultList = result.getMappedResults();
Aggregation Framework Example 6
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 with indexer expressions according to their position. In this example, we reference the first parameter of the parameters array with [0]
. When the SpEL expression is transformed into a MongoDB aggregation framework expression, external parameter expressions are replaced with their respective values.
class Product {
String id;
String name;
double netPrice;
int spaceUnits;
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
double shippingCosts = 1.2;
TypedAggregation<Product> agg = newAggregation(Product.class,
project("name", "netPrice")
.andExpression("(netPrice * (1-discountRate) + [0]) * (1+taxRate)", shippingCosts).as("salesPrice")
);
AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);
List<Document> resultList = result.getMappedResults();
Note that we can also refer to other fields of the document within the SpEL expression.
Aggregation Framework Example 7
This example uses conditional projection. It is derived from the $cond reference documentation.
public class InventoryItem {
@Id int id;
String item;
String description;
int qty;
}
public class InventoryItemProjection {
@Id int id;
String item;
String description;
int qty;
int discount
}
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
TypedAggregation<InventoryItem> agg = newAggregation(InventoryItem.class,
project("item").and("discount")
.applyCondition(ConditionalOperator.newBuilder().when(Criteria.where("qty").gte(250))
.then(30)
.otherwise(20))
.and(ifNull("description", "Unspecified")).as("description")
);
AggregationResults<InventoryItemProjection> result = mongoTemplate.aggregate(agg, "inventory", InventoryItemProjection.class);
List<InventoryItemProjection> stateStatsList = result.getMappedResults();
This one-step aggregation uses a projection operation with the inventory
collection. We project the discount
field by using a conditional operation for all inventory items that have a qty
greater than or equal to 250
. A second conditional projection is performed for the description
field. We apply the Unspecified
description to all items that either do not have a description
field or items that have a null
description.
As of MongoDB 3.6, it is possible to exclude fields from the projection by using a conditional expression.
TypedAggregation<Book> agg = Aggregation.newAggregation(Book.class,
project("title")
.and(ConditionalOperators.when(ComparisonOperators.valueOf("author.middle") (1)
.equalToValue("")) (2)
.then("$$REMOVE") (3)
.otherwiseValueOf("author.middle") (4)
)
.as("author.middle"));
1 | If the value of the field author.middle |
2 | does not contain a value, |
3 | then use $$REMOVE to exclude the field. |
4 | Otherwise, add the field value of author.middle . |
11.13. Index and Collection Management
MongoTemplate
provides a few methods for managing indexes and collections. These methods are collected into a helper interface called IndexOperations
. You can access these operations by calling the indexOps
method and passing in either the collection name or the java.lang.Class
of your entity (the collection name is derived from the .class
, either by name or from annotation metadata).
The following listing shows the IndexOperations
interface:
public interface IndexOperations {
void ensureIndex(IndexDefinition indexDefinition);
void dropIndex(String name);
void dropAllIndexes();
void resetIndexCache();
List<IndexInfo> getIndexInfo();
}
11.13.1. Methods for Creating an Index
You can create an index on a collection to improve query performance by using the MongoTemplate class, as the following example shows:
mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING));
ensureIndex
makes sure that an index for the provided IndexDefinition exists for the collection.
You can create standard, geospatial, and text indexes by using the IndexDefinition
, GeoSpatialIndex
and TextIndexDefinition
classes. For example, given the Venue
class defined in a previous section, you could declare a geospatial query, as the following example shows:
mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));
Index and GeospatialIndex support configuration of collations.
|
11.13.2. Accessing Index Information
The IndexOperations
interface has the getIndexInfo
method that returns a list of IndexInfo
objects. This list contains all the indexes defined on the collection. The following example defines an index on the Person
class that has an age
property:
template.indexOps(Person.class).ensureIndex(new Index().on("age", Order.DESCENDING).unique());
List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
// Contains
// [IndexInfo [fieldSpec={_id=ASCENDING}, name=_id_, unique=false, sparse=false],
// IndexInfo [fieldSpec={age=DESCENDING}, name=age_-1, unique=true, sparse=false]]
11.13.3. Methods for Working with a Collection
The following example shows how to create a collection:
MongoTemplate
MongoCollection<Document> collection = null;
if (!mongoTemplate.getCollectionNames().contains("MyNewCollection")) {
collection = mongoTemplate.createCollection("MyNewCollection");
}
mongoTemplate.dropCollection("MyNewCollection");
-
getCollectionNames: Returns a set of collection names.
-
collectionExists: Checks to see if a collection with a given name exists.
-
createCollection: Creates an uncapped collection.
-
dropCollection: Drops the collection.
-
getCollection: Gets a collection by name, creating it if it does not exist.
Collection creation allows customization with CollectionOptions and supports collations.
|
11.14. Running Commands
You can get at the MongoDB driver’s MongoDatabase.runCommand( )
method by using the executeCommand(…)
methods on MongoTemplate
. These methods also perform exception translation into Spring’s DataAccessException
hierarchy.
11.14.1. Methods for running commands
-
Document
executeCommand(Document command)
: Run a MongoDB command. -
Document
executeCommand(Document command, ReadPreference readPreference)
: Run a MongoDB command with the given nullable MongoDBReadPreference
. -
Document
executeCommand(String jsonCommand)
: Run a MongoDB command expressed as a JSON string.
11.15. Lifecycle Events
The MongoDB mapping framework includes several org.springframework.context.ApplicationEvent
events that your application can respond to by registering special beans in the ApplicationContext
. Being based on Spring’s ApplicationContext
event infrastructure enables other products, such as Spring Integration, to easily receive these events, as they are a well known eventing mechanism in Spring-based applications.
To intercept an object before it goes through the conversion process (which turns your domain object into a org.bson.Document
), you can register a subclass of AbstractMongoEventListener
that overrides the onBeforeConvert
method. When the event is dispatched, your listener is called and passed the domain object before it goes into the converter. The following example shows how to do so:
public class BeforeConvertListener extends AbstractMongoEventListener<Person> {
@Override
public void onBeforeConvert(BeforeConvertEvent<Person> event) {
... does some auditing manipulation, set timestamps, whatever ...
}
}
To intercept an object before it goes into the database, you can register a subclass of org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener
that overrides the onBeforeSave
method. When the event is dispatched, your listener is called and passed the domain object and the converted com.mongodb.Document
. The following example shows how to do so:
public class BeforeSaveListener extends AbstractMongoEventListener<Person> {
@Override
public void onBeforeSave(BeforeSaveEvent<Person> event) {
… change values, delete them, whatever …
}
}
Declaring these beans in your Spring ApplicationContext causes them to be invoked whenever the event is dispatched.
The following callback methods are present in AbstractMappingEventListener
:
-
onBeforeConvert
: Called inMongoTemplate
insert
,insertList
, andsave
operations before the object is converted to aDocument
by aMongoConverter
. -
onBeforeSave
: Called inMongoTemplate
insert
,insertList
, andsave
operations before inserting or saving theDocument
in the database. -
onAfterSave
: Called inMongoTemplate
insert
,insertList
, andsave
operations after inserting or saving theDocument
in the database. -
onAfterLoad
: Called inMongoTemplate
find
,findAndRemove
,findOne
, andgetCollection
methods after theDocument
has been retrieved from the database. -
onAfterConvert
: Called inMongoTemplate
find
,findAndRemove
,findOne
, andgetCollection
methods after theDocument
has been retrieved from the database was converted to a POJO.
Lifecycle events are only emitted for root level types. Complex types used as properties within a document root are not subject to event publication unless they are document references annotated with @DBRef .
|
Lifecycle events depend on an ApplicationEventMulticaster , which in case of the SimpleApplicationEventMulticaster can be configured with a TaskExecutor , and therefore gives no guarantees when an Event is processed.
|
11.16. Entity Callbacks
The Spring Data infrastructure provides hooks for modifying an entity before and after certain methods are invoked.
Those so called EntityCallback
instances provide a convenient way to check and potentially modify an entity in a callback fashioned style.
An EntityCallback
looks pretty much like a specialized ApplicationListener
.
Some Spring Data modules publish store specific events (such as BeforeSaveEvent
) that allow modifying the given entity. In some cases, such as when working with immutable types, these events can cause trouble.
Also, event publishing relies on ApplicationEventMulticaster
. If configuring that with an asynchronous TaskExecutor
it can lead to unpredictable outcomes, as event processing can be forked onto a Thread.
Entity callbacks provide integration points with both synchronous and reactive APIs to guarantee in-order execution at well-defined checkpoints within the processing chain, returning a potentially modified entity or an reactive wrapper type.
Entity callbacks are typically separated by API type. This separation means that a synchronous API considers only synchronous entity callbacks and a reactive implementation considers only reactive entity callbacks.
The Entity Callback API has been introduced with Spring Data Commons 2.2. It is the recommended way of applying entity modifications.
Existing store specific |
11.16.1. Implementing Entity Callbacks
An EntityCallback
is directly associated with its domain type through its generic type argument.
Each Spring Data module typically ships with a set of predefined EntityCallback
interfaces covering the entity lifecycle.
EntityCallback
@FunctionalInterface
public interface BeforeSaveCallback<T> extends EntityCallback<T> {
/**
* Entity callback method invoked before a domain object is saved.
* Can return either the same or a modified instance.
*
* @return the domain object to be persisted.
*/
T onBeforeSave(T entity <2>, String collection <3>); (1)
}
1 | BeforeSaveCallback specific method to be called before an entity is saved. Returns a potentially modifed instance. |
2 | The entity right before persisting. |
3 | A number of store specific arguments like the collection the entity is persisted to. |
EntityCallback
@FunctionalInterface
public interface ReactiveBeforeSaveCallback<T> extends EntityCallback<T> {
/**
* Entity callback method invoked on subscription, before a domain object is saved.
* The returned Publisher can emit either the same or a modified instance.
*
* @return Publisher emitting the domain object to be persisted.
*/
Publisher<T> onBeforeSave(T entity <2>, String collection <3>); (1)
}
1 | BeforeSaveCallback specific method to be called on subscription, before an entity is saved. Emits a potentially modifed instance. |
2 | The entity right before persisting. |
3 | A number of store specific arguments like the collection the entity is persisted to. |
Optional entity callback parameters are defined by the implementing Spring Data module and inferred from call site of EntityCallback.callback() .
|
Implement the interface suiting your application needs like shown in the example below:
BeforeSaveCallback
class DefaultingEntityCallback implements BeforeSaveCallback<Person>, Ordered { (2)
@Override
public Object onBeforeSave(Person entity, String collection) { (1)
if(collection == "user") {
return // ...
}
return // ...
}
@Override
public int getOrder() {
return 100; (2)
}
}
1 | Callback implementation according to your requirements. |
2 | Potentially order the entity callback if multiple ones for the same domain type exist. Ordering follows lowest precedence. |
11.16.2. Registering Entity Callbacks
EntityCallback
beans are picked up by the store specific implementations in case they are registered in the ApplicationContext
.
Most template APIs already implement ApplicationContextAware
and therefore have access to the ApplicationContext
The following example explains a collection of valid entity callback registrations:
EntityCallback
Bean registration@Order(1) (1)
@Component
class First implements BeforeSaveCallback<Person> {
@Override
public Person onBeforeSave(Person person) {
return // ...
}
}
@Component
class DefaultingEntityCallback implements BeforeSaveCallback<Person>,
Ordered { (2)
@Override
public Object onBeforeSave(Person entity, String collection) {
// ...
}
@Override
public int getOrder() {
return 100; (2)
}
}
@Configuration
public class EntityCallbackConfiguration {
@Bean
BeforeSaveCallback<Person> unorderedLambdaReceiverCallback() { (3)
return (BeforeSaveCallback<Person>) it -> // ...
}
}
@Component
class UserCallbacks implements BeforeConvertCallback<User>,
BeforeSaveCallback<User> { (4)
@Override
public Person onBeforeConvert(User user) {
return // ...
}
@Override
public Person onBeforeSave(User user) {
return // ...
}
}
1 | BeforeSaveCallback receiving its order from the @Order annotation. |
2 | BeforeSaveCallback receiving its order via the Ordered interface implementation. |
3 | BeforeSaveCallback using a lambda expression. Unordered by default and invoked last. Note that callbacks implemented by a lambda expression do not expose typing information hence invoking these with a non-assignable entity affects the callback throughput. Use a class or enum to enable type filtering for the callback bean. |
4 | Combine multiple entity callback interfaces in a single implementation class. |
11.16.3. Store specific EntityCallbacks
Spring Data MongoDB uses the EntityCallback
API for its auditing support and reacts on the following callbacks.
Callback | Method | Description | Order |
---|---|---|---|
Reactive/BeforeConvertCallback |
|
Invoked before a domain object is converted to |
|
Reactive/AfterConvertCallback |
|
Invoked after a domain object is loaded. |
|
Reactive/AuditingEntityCallback |
|
Marks an auditable entity created or modified |
100 |
Reactive/BeforeSaveCallback |
|
Invoked before a domain object is saved. |
|
Reactive/AfterSaveCallback |
|
Invoked before a domain object is saved. |
|
11.17. Exception Translation
The Spring framework provides exception translation for a wide variety of database and mapping technologies. This has traditionally been for JDBC and JPA. The Spring support for MongoDB extends this feature to the MongoDB Database by providing an implementation of the org.springframework.dao.support.PersistenceExceptionTranslator
interface.
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 that you can be sure 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 that no information is lost.
Some of the mappings performed by the MongoExceptionTranslator
are com.mongodb.Network to DataAccessResourceFailureException
and MongoException
error codes 1003, 12001, 12010, 12011, and 12012 to InvalidDataAccessApiUsageException
. Look into the implementation for more details on the mapping.
11.18. Execution Callbacks
One common design feature of all Spring template classes is that all functionality is routed into one of the template’s execute
callback methods. Doing so helps to ensure that exceptions and any resource management that may be required are performed consistently. While JDBC and JMS need this feature much more than MongoDB does, it still offers a single spot for exception translation and logging to occur. Consequently, using these execute
callbacks is the preferred way to access the MongoDB driver’s MongoDatabase
and MongoCollection
objects to perform uncommon operations that were not exposed as methods on MongoTemplate
.
The following list describes the execute
callback methods.
-
<T> T
execute(Class<?> entityClass, CollectionCallback<T> action)
: Runs the givenCollectionCallback
for the entity collection of the specified class. -
<T> T
execute(String collectionName, CollectionCallback<T> action)
: Runs the givenCollectionCallback
on the collection of the given name. -
<T> T
execute(DbCallback<T> action)
: Runs a DbCallback, translating any exceptions as necessary. Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2. -
<T> T
execute(String collectionName, DbCallback<T> action)
: Runs aDbCallback
on the collection of the given name translating any exceptions as necessary. -
<T> T
executeInSession(DbCallback<T> action)
: Runs the givenDbCallback
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.
The following example uses the CollectionCallback
to return information about an index:
boolean hasIndex = template.execute("geolocation", new CollectionCallbackBoolean>() {
public Boolean doInCollection(Venue.class, DBCollection collection) throws MongoException, DataAccessException {
List<Document> indexes = collection.getIndexInfo();
for (Document document : indexes) {
if ("location_2d".equals(document.get("name"))) {
return true;
}
}
return false;
}
});
11.19. GridFS Support
MongoDB supports storing binary files inside its filesystem, GridFS. Spring Data MongoDB provides a GridFsOperations
interface as well as the corresponding implementation, GridFsTemplate
, to let you interact with the filesystem. You can set up a GridFsTemplate
instance by handing it a MongoDatabaseFactory
as well as a MongoConverter
, as the following example shows:
class GridFsConfiguration extends AbstractMongoClientConfiguration {
// … further configuration omitted
@Bean
public GridFsTemplate gridFsTemplate() {
return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
}
}
The corresponding XML configuration follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<mongo:db-factory id="mongoDbFactory" dbname="database" />
<mongo:mapping-converter id="converter" />
<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, as the following example shows:
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void storeFileToGridFs() {
FileMetadata metadata = new FileMetadata();
// populate metadata
Resource file = … // lookup File or Resource
operations.store(file.getInputStream(), "filename.txt", metadata);
}
}
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 marshaled by the MongoConverter
configured with the GridFsTemplate
. Alternatively, you can also provide a Document
.
You can read files from the filesystem through either the find(…)
or the getResources(…)
methods. Let’s have a look at the find(…)
methods first. You can either find a single file or multiple files that match a Query
. You can use the GridFsCriteria
helper class to define queries. It provides static factory methods to encapsulate default metadata fields (such as whereFilename()
and whereContentType()
) or a custom one through whereMetaData()
. The following example shows how to use GridFsTemplate
to query for files:
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void findFilesInGridFs() {
GridFSFindIterable result = operations.find(query(whereFilename().is("filename.txt")))
}
}
Currently, MongoDB does not support defining sort criteria when retrieving files from GridFS. For this reason, any sort criteria defined on the Query instance handed into the find(…) method are disregarded.
|
The other option to read files from the GridFs is to use the methods introduced by the ResourcePatternResolver
interface. They allow handing an Ant path into the method and can thus retrieve files matching the given pattern. The following example shows how to use GridFsTemplate
to read files:
class GridFsClient {
@Autowired
GridFsOperations operations;
@Test
public void readFilesFromGridFs() {
GridFsResources[] txtFiles = operations.getResources("*.txt");
}
}
GridFsOperations
extends ResourcePatternResolver
and lets the GridFsTemplate
(for example) to be plugged into an ApplicationContext
to read Spring Config files from MongoDB database.
11.20. Infinite Streams with Tailable Cursors
By default, MongoDB automatically closes a cursor when the client exhausts all results supplied by the cursor. Closing a cursor on exhaustion turns a stream into a finite stream. For capped collections, you can use a Tailable Cursor that remains open after the client consumed all initially returned data.
Capped collections can be created with MongoOperations.createCollection . To do so, provide the required CollectionOptions.empty().capped()… .
|
Tailable cursors can be consumed with both, the imperative and the reactive MongoDB API. It is highly recommended to use the reactive variant, as it is less resource-intensive. However, if you cannot use the reactive API, you can still use a messaging concept that is already prevalent in the Spring ecosystem.
11.20.1. Tailable Cursors with MessageListener
Listening to a capped collection using a Sync Driver creates a long running, blocking task that needs to be delegated to
a separate component. In this case, we need to first create a MessageListenerContainer
, which will be the main entry point
for running the specific SubscriptionRequest
. Spring Data MongoDB already ships with a default implementation that
operates on MongoTemplate
and is capable of creating and running Task
instances for a TailableCursorRequest
.
The following example shows how to use tailable cursors with MessageListener
instances:
MessageListener
instancesMessageListenerContainer container = new DefaultMessageListenerContainer(template);
container.start(); (1)
MessageListener<Document, User> listener = System.out::println; (2)
TailableCursorRequest request = TailableCursorRequest.builder()
.collection("orders") (3)
.filter(query(where("value").lt(100))) (4)
.publishTo(listener) (5)
.build();
container.register(request, User.class); (6)
// ...
container.stop(); (7)
1 | Starting the container intializes the resources and starts Task instances for already registered SubscriptionRequest instances. Requests added after startup are ran immediately. |
2 | Define the listener called when a Message is received. The Message#getBody() is converted to the requested domain type. Use Document to receive raw results without conversion. |
3 | Set the collection to listen to. |
4 | Provide an optional filter for documents to receive. |
5 | Set the message listener to publish incoming Message s to. |
6 | Register the request. The returned Subscription can be used to check the current Task state and cancel it to free resources. |
7 | Do not forget to stop the container once you are sure you no longer need it. Doing so stops all running Task instances within the container. |
11.20.2. Reactive Tailable Cursors
Using tailable cursors with a reactive data types allows construction of infinite streams. A tailable cursor remains open until it is closed externally. It emits data as new documents arrive in a capped collection.
Tailable cursors may become dead, or invalid, if either the query returns no match or the cursor returns the document at the “end” of the collection and the application then deletes that document. The following example shows how to create and use an infinite stream query:
Flux<Person> stream = template.tail(query(where("name").is("Joe")), Person.class);
Disposable subscription = stream.doOnNext(person -> System.out.println(person)).subscribe();
// …
// Later: Dispose the subscription to close the stream
subscription.dispose();
Spring Data MongoDB Reactive repositories support infinite streams by annotating a query method with @Tailable
. This works for methods that return Flux
and other reactive types capable of emitting multiple elements, as the following example shows:
public interface PersonRepository extends ReactiveMongoRepository<Person, String> {
@Tailable
Flux<Person> findByFirstname(String firstname);
}
Flux<Person> stream = repository.findByFirstname("Joe");
Disposable subscription = stream.doOnNext(System.out::println).subscribe();
// …
// Later: Dispose the subscription to close the stream
subscription.dispose();
11.21. Change Streams
As of MongoDB 3.6, Change Streams let applications get notified about changes without having to tail the oplog.
Change Stream support is only possible for replica sets or for a sharded cluster. |
Change Streams can be consumed with both, the imperative and the reactive MongoDB Java driver. It is highly recommended to use the reactive variant, as it is less resource-intensive. However, if you cannot use the reactive API, you can still obtain change events by using the messaging concept that is already prevalent in the Spring ecosystem.
It is possible to watch both on a collection as well as database level, whereas the database level variant publishes
changes from all collections within the database. When subscribing to a database change stream, make sure to use a
suitable type for the event type as conversion might not apply correctly across different entity types.
In doubt, use Document
.
11.21.1. Change Streams with MessageListener
Listening to a Change Stream by using a Sync Driver creates a long running, blocking task that needs to be delegated to a separate component.
In this case, we need to first create a MessageListenerContainer
, which will be the main entry point for running the specific SubscriptionRequest
tasks.
Spring Data MongoDB already ships with a default implementation that operates on MongoTemplate
and is capable of creating and running Task
instances for a ChangeStreamRequest
.
The following example shows how to use Change Streams with MessageListener
instances:
MessageListener
instancesMessageListenerContainer container = new DefaultMessageListenerContainer(template);
container.start(); (1)
MessageListener<ChangeStreamDocument<Document>, User> listener = System.out::println; (2)
ChangeStreamRequestOptions options = new ChangeStreamRequestOptions("user", ChangeStreamOptions.empty()); (3)
Subscription subscription = container.register(new ChangeStreamRequest<>(listener, options), User.class); (4)
// ...
container.stop(); (5)
1 | Starting the container initializes the resources and starts Task instances for already registered SubscriptionRequest instances. Requests added after startup are ran immediately. |
2 | Define the listener called when a Message is received. The Message#getBody() is converted to the requested domain type. Use Document to receive raw results without conversion. |
3 | Set the collection to listen to and provide additional options through ChangeStreamOptions . |
4 | Register the request. The returned Subscription can be used to check the current Task state and cancel it to free resources. |
5 | Do not forget to stop the container once you are sure you no longer need it. Doing so stops all running Task instances within the container. |
Errors while processing are passed on to an |
11.21.2. Reactive Change Streams
Subscribing to Change Streams with the reactive API is a more natural approach to work with streams. Still, the essential building blocks, such as ChangeStreamOptions
, remain the same. The following example shows how to use Change Streams emitting ChangeStreamEvent
s:
ChangeStreamEvent
Flux<ChangeStreamEvent<User>> flux = reactiveTemplate.changeStream(User.class) (1)
.watchCollection("people")
.filter(where("age").gte(38)) (2)
.listen(); (3)
1 | The event target type the underlying document should be converted to. Leave this out to receive raw results without conversion. |
2 | Use an aggregation pipeline or just a query Criteria to filter events. |
3 | Obtain a Flux of change stream events. The ChangeStreamEvent#getBody() is converted to the requested domain type from (2). |
11.21.3. Resuming Change Streams
Change Streams can be resumed and resume emitting events where you left. To resume the stream, you need to supply either a resume
token or the last known server time (in UTC). Use ChangeStreamOptions
to set the value accordingly.
The following example shows how to set the resume offset using server time:
Flux<ChangeStreamEvent<User>> resumed = template.changeStream(User.class)
.watchCollection("people")
.resumeAt(Instant.now().minusSeconds(1)) (1)
.listen();
1 | You may obtain the server time of an ChangeStreamEvent through the getTimestamp method or use the resumeToken
exposed through getResumeToken . |
In some cases an Instant might not be a precise enough measure when resuming a Change Stream. Use a MongoDB native
BsonTimestamp for that purpose.
|
11.22. Time Series
MongoDB 5.0 introduced Time Series collections that are optimized to efficiently store documents over time such as measurements or events.
Those collections need to be created as such before inserting any data.
Collections can be created by either running the createCollection
command, defining time series collection options or extracting options from a @TimeSeries
annotation as shown in the examples below.
template.execute(db -> { com.mongodb.client.model.CreateCollectionOptions options = new CreateCollectionOptions(); options.timeSeriesOptions(new TimeSeriesOptions("timestamp")); db.createCollection("weather", options); return "OK"; });
CollectionOptions
template.createCollection("weather", CollectionOptions.timeSeries("timestamp"));
@TimeSeries(collection="weather", timeField = "timestamp") public class Measurement { String id; Instant timestamp; // ... } template.createCollection(Measurement.class);
The snippets above can easily be transferred to the reactive API offering the very same methods. Make sure to properly subscribe to the returned publishers.
12. MongoDB Sessions
As of version 3.6, MongoDB supports the concept of sessions. The use of sessions enables MongoDB’s Causal Consistency model, which guarantees running operations in an order that respects their causal relationships. Those are split into ServerSession
instances and ClientSession
instances. In this section, when we speak of a session, we refer to ClientSession
.
Operations within a client session are not isolated from operations outside the session. |
Both MongoOperations
and ReactiveMongoOperations
provide gateway methods for tying a ClientSession
to the operations. MongoCollection
and MongoDatabase
use session proxy objects that implement MongoDB’s collection and database interfaces, so you need not add a session on each call. This means that a potential call to MongoCollection#find()
is delegated to MongoCollection#find(ClientSession)
.
Methods such as (Reactive)MongoOperations#getCollection return native MongoDB Java Driver gateway objects (such as MongoCollection ) that themselves offer dedicated methods for ClientSession . These methods are NOT session-proxied. You should provide the ClientSession where needed when interacting directly with a MongoCollection or MongoDatabase and not through one of the #execute callbacks on MongoOperations .
|
12.1. Synchronous ClientSession
support.
The following example shows the usage of a session:
ClientSession
with MongoOperations
ClientSessionOptions sessionOptions = ClientSessionOptions.builder()
.causallyConsistent(true)
.build();
ClientSession session = client.startSession(sessionOptions); (1)
template.withSession(() -> session)
.execute(action -> {
Query query = query(where("name").is("Durzo Blint"));
Person durzo = action.findOne(query, Person.class); (2)
Person azoth = new Person("Kylar Stern");
azoth.setMaster(durzo);
action.insert(azoth); (3)
return azoth;
});
session.close() (4)
1 | Obtain a new session from the server. |
2 | Use MongoOperation methods as before. The ClientSession gets applied automatically. |
3 | Make sure to close the ClientSession . |
4 | Close the session. |
When dealing with DBRef instances, especially lazily loaded ones, it is essential to not close the ClientSession before all data is loaded. Otherwise, lazy fetch fails.
|
12.2. Reactive ClientSession
support
The reactive counterpart uses the same building blocks as the imperative one, as the following example shows:
ReactiveMongoOperations
ClientSessionOptions sessionOptions = ClientSessionOptions.builder()
.causallyConsistent(true)
.build();
Publisher<ClientSession> session = client.startSession(sessionOptions); (1)
template.withSession(session)
.execute(action -> {
Query query = query(where("name").is("Durzo Blint"));
return action.findOne(query, Person.class)
.flatMap(durzo -> {
Person azoth = new Person("Kylar Stern");
azoth.setMaster(durzo);
return action.insert(azoth); (2)
});
}, ClientSession::close) (3)
.subscribe(); (4)
1 | Obtain a Publisher for new session retrieval. |
2 | Use ReactiveMongoOperation methods as before. The ClientSession is obtained and applied automatically. |
3 | Make sure to close the ClientSession . |
4 | Nothing happens until you subscribe. See the Project Reactor Reference Guide for details. |
By using a Publisher
that provides the actual session, you can defer session acquisition to the point of actual subscription.
Still, you need to close the session when done, so as to not pollute the server with stale sessions. Use the doFinally
hook on execute
to call ClientSession#close()
when you no longer need the session.
If you prefer having more control over the session itself, you can obtain the ClientSession
through the driver and provide it through a Supplier
.
Reactive use of ClientSession is limited to Template API usage. There’s currently no session integration with reactive repositories.
|
13. MongoDB Transactions
As of version 4, MongoDB supports Transactions. Transactions are built on top of Sessions and, consequently, require an active ClientSession
.
Unless you specify a MongoTransactionManager within your application context, transaction support is DISABLED. You can use setSessionSynchronization(ALWAYS) to participate in ongoing non-native MongoDB transactions.
|
To get full programmatic control over transactions, you may want to use the session callback on MongoOperations
.
The following example shows programmatic transaction control within a SessionCallback
:
ClientSession session = client.startSession(options); (1)
template.withSession(session)
.execute(action -> {
session.startTransaction(); (2)
try {
Step step = // ...;
action.insert(step);
process(step);
action.update(Step.class).apply(Update.set("state", // ...
session.commitTransaction(); (3)
} catch (RuntimeException e) {
session.abortTransaction(); (4)
}
}, ClientSession::close) (5)
1 | Obtain a new ClientSession . |
2 | Start the transaction. |
3 | If everything works out as expected, commit the changes. |
4 | Something broke, so roll back everything. |
5 | Do not forget to close the session when done. |
The preceding example lets you have full control over transactional behavior while using the session scoped MongoOperations
instance within the callback to ensure the session is passed on to every server call.
To avoid some of the overhead that comes with this approach, you can use a TransactionTemplate
to take away some of the noise of manual transaction flow.
13.1. Transactions with TransactionTemplate
Spring Data MongoDB transactions support a TransactionTemplate
. The following example shows how to create and use a TransactionTemplate
:
TransactionTemplate
template.setSessionSynchronization(ALWAYS); (1)
// ...
TransactionTemplate txTemplate = new TransactionTemplate(anyTxManager); (2)
txTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) { (3)
Step step = // ...;
template.insert(step);
process(step);
template.update(Step.class).apply(Update.set("state", // ...
};
});
1 | Enable transaction synchronization during Template API configuration. |
2 | Create the TransactionTemplate using the provided PlatformTransactionManager . |
3 | Within the callback the ClientSession and transaction are already registered. |
Changing state of MongoTemplate during runtime (as you might think would be possible in item 1 of the preceding listing) can cause threading and visibility issues.
|
13.2. Transactions with MongoTransactionManager
MongoTransactionManager
is the gateway to the well known Spring transaction support. It lets applications use the managed transaction features of Spring.
The MongoTransactionManager
binds a ClientSession
to the thread. MongoTemplate
detects the session and operates on these resources which are associated with the transaction accordingly. MongoTemplate
can also participate in other, ongoing transactions. The following example shows how to create and use transactions with a MongoTransactionManager
:
MongoTransactionManager
@Configuration
static class Config extends AbstractMongoClientConfiguration {
@Bean
MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) { (1)
return new MongoTransactionManager(dbFactory);
}
// ...
}
@Component
public class StateService {
@Transactional
void someBusinessFunction(Step step) { (2)
template.insert(step);
process(step);
template.update(Step.class).apply(Update.set("state", // ...
};
});
1 | Register MongoTransactionManager in the application context. |
2 | Mark methods as transactional. |
@Transactional(readOnly = true) advises MongoTransactionManager to also start a transaction that adds the
ClientSession to outgoing requests.
|
13.3. Reactive Transactions
Same as with the reactive ClientSession
support, the ReactiveMongoTemplate
offers dedicated methods for operating
within a transaction without having to worry about the committing or stopping actions depending on the operations outcome.
Unless you specify a ReactiveMongoTransactionManager within your application context, transaction support is DISABLED. You can use setSessionSynchronization(ALWAYS) to participate in ongoing non-native MongoDB transactions.
|
Using the plain MongoDB reactive driver API a delete
within a transactional flow may look like this.
Mono<DeleteResult> result = Mono
.from(client.startSession()) (1)
.flatMap(session -> {
session.startTransaction(); (2)
return Mono.from(collection.deleteMany(session, ...)) (3)
.onErrorResume(e -> Mono.from(session.abortTransaction()).then(Mono.error(e))) (4)
.flatMap(val -> Mono.from(session.commitTransaction()).then(Mono.just(val))) (5)
.doFinally(signal -> session.close()); (6)
});
1 | First we obviously need to initiate the session. |
2 | Once we have the ClientSession at hand, start the transaction. |
3 | Operate within the transaction by passing on the ClientSession to the operation. |
4 | If the operations completes exceptionally, we need to stop the transaction and preserve the error. |
5 | Or of course, commit the changes in case of success. Still preserving the operations result. |
6 | Lastly, we need to make sure to close the session. |
The culprit of the above operation is in keeping the main flows DeleteResult
instead of the transaction outcome
published via either commitTransaction()
or abortTransaction()
, which leads to a rather complicated setup.
13.4. Transactions with TransactionalOperator
Spring Data MongoDB transactions support a TransactionalOperator
. The following example shows how to create and use a TransactionalOperator
:
TransactionalOperator
template.setSessionSynchronization(ALWAYS); (1)
// ...
TransactionalOperator rxtx = TransactionalOperator.create(anyTxManager,
new DefaultTransactionDefinition()); (2)
Step step = // ...;
template.insert(step);
Mono<Void> process(step)
.then(template.update(Step.class).apply(Update.set("state", …))
.as(rxtx::transactional) (3)
.then();
1 | Enable transaction synchronization for Transactional participation. |
2 | Create the TransactionalOperator using the provided ReactiveTransactionManager . |
3 | TransactionalOperator.transactional(…) provides transaction management for all upstream operations. |
13.5. Transactions with ReactiveMongoTransactionManager
ReactiveMongoTransactionManager
is the gateway to the well known Spring transaction support.
It allows applications to leverage the managed transaction features of Spring.
The ReactiveMongoTransactionManager
binds a ClientSession
to the subscriber Context
.
ReactiveMongoTemplate
detects the session and operates on these resources which are associated with the transaction accordingly.
ReactiveMongoTemplate
can also participate in other, ongoing transactions.
The following example shows how to create and use transactions with a ReactiveMongoTransactionManager
:
ReactiveMongoTransactionManager
@Configuration
static class Config extends AbstractMongoClientConfiguration {
@Bean
ReactiveMongoTransactionManager transactionManager(ReactiveDatabaseFactory factory) { (1)
return new ReactiveMongoTransactionManager(factory);
}
// ...
}
@Service
public class StateService {
@Transactional
Mono<UpdateResult> someBusinessFunction(Step step) { (2)
return template.insert(step)
.then(process(step))
.then(template.update(Step.class).apply(Update.set("state", …));
};
});
1 | Register ReactiveMongoTransactionManager in the application context. |
2 | Mark methods as transactional. |
@Transactional(readOnly = true) advises ReactiveMongoTransactionManager to also start a transaction that adds the ClientSession to outgoing requests.
|
13.6. Special behavior inside transactions
Inside transactions, MongoDB server has a slightly different behavior.
Connection Settings
The MongoDB drivers offer a dedicated replica set name configuration option turing the driver into auto detection mode. This option helps identifying the primary replica set nodes and command routing during a transaction.
Make sure to add replicaSet to the MongoDB URI. Please refer to connection string options for further details.
|
Collection Operations
MongoDB does not support collection operations, such as collection creation, within a transaction. This also affects the on the fly collection creation that happens on first usage. Therefore make sure to have all required structures in place.
Transient Errors
MongoDB can add special labels to errors raised during transactional operations. Those may indicate transient failures
that might vanish by merely retrying the operation.
We highly recommend Spring Retry for those purposes. Nevertheless
one may override MongoTransactionManager#doCommit(MongoTransactionObject)
to implement a Retry Commit Operation
behavior as outlined in the MongoDB reference manual.
Count
MongoDB count
operates upon collection statistics which may not reflect the actual situation within a transaction.
The server responds with error 50851 when issuing a count
command inside of a multi-document transaction.
Once MongoTemplate
detects an active transaction, all exposed count()
methods are converted and delegated to the
aggregation framework using $match
and $count
operators, preserving Query
settings, such as collation
.
Restrictions apply when using geo commands inside of the aggregation count helper. The following operators cannot be used and must be replaced with a different operator:
-
$where
→$expr
-
$near
→$geoWithin
with$center
-
$nearSphere
→$geoWithin
with$centerSphere
Queries using Criteria.near(…)
and Criteria.nearSphere(…)
must be rewritten to Criteria.within(…)
respective Criteria.withinSphere(…)
. Same applies for the near
query keyword in repository query methods that must be changed to within
. See also MongoDB JIRA ticket DRIVERS-518 for further reference.
The following snippet shows count
usage inside the session-bound closure:
session.startTransaction();
template.withSession(session)
.execute(action -> {
action.count(query(where("state").is("active")), Step.class)
...
The snippet above materializes in the following command:
db.collection.aggregate(
[
{ $match: { state: "active" } },
{ $count: "totalEntityCount" }
]
)
instead of:
db.collection.find( { state: "active" } ).count()
14. Reactive MongoDB support
The reactive MongoDB support contains the following basic set of features:
-
Spring configuration support that uses Java-based
@Configuration
classes, aMongoClient
instance, and replica sets. -
ReactiveMongoTemplate
, which is a helper class that increases productivity by usingMongoOperations
in a reactive manner. It includes integrated object mapping betweenDocument
instances and POJOs. -
Exception translation into Spring’s portable Data Access Exception hierarchy.
-
Feature-rich Object Mapping integrated with Spring’s
ConversionService
. -
Annotation-based mapping metadata that is extensible to support other metadata formats.
-
Persistence and mapping lifecycle events.
-
Java based
Query
,Criteria
, andUpdate
DSLs. -
Automatic implementation of reactive repository interfaces including support for custom query methods.
For most tasks, you should use ReactiveMongoTemplate
or the repository support, both of which use the rich mapping functionality. ReactiveMongoTemplate
is the place to look for accessing functionality such as incrementing counters or ad-hoc CRUD operations. ReactiveMongoTemplate
also provides callback methods so that you can use the low-level API artifacts (such as MongoDatabase
) 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 that you can map your existing knowledge onto the Spring APIs.
14.1. Getting Started
Spring MongoDB support requires MongoDB 2.6 or higher and Java SE 8 or higher.
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 running 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 and press Yes when prompted. Then enter a project and a package name, such as org.spring.mongodb.example.
Then add the following to the pom.xml dependencies section.
<dependencies>
<!-- other dependency elements omitted -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>2020.0.21</version>
</dependency>
</dependencies>
MongoDB uses two different drivers for blocking and reactive (non-blocking) data access. While blocking operations are provided by default, you can opt-in for reactive usage. |
To get started with a working example, create a simple Person
class to persist, as follows:
@Document
public class Person {
private String id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
Then create an application to run, as follows:
public class ReactiveMongoApp {
private static final Logger log = LoggerFactory.getLogger(ReactiveMongoApp.class);
public static void main(String[] args) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
ReactiveMongoTemplate mongoOps = new ReactiveMongoTemplate(MongoClients.create(), "database");
mongoOps.insert(new Person("Joe", 34))
.flatMap(p -> mongoOps.findOne(new Query(where("name").is("Joe")), Person.class))
.doOnNext(person -> log.info(person.toString()))
.flatMap(person -> mongoOps.dropCollection("person"))
.doOnComplete(latch::countDown)
.subscribe();
latch.await();
}
}
Running the preceding class produces the following output:
2016-09-20 14:56:57,373 DEBUG .index.MongoPersistentEntityIndexCreator: 124 - Analyzing class class example.ReactiveMongoApp$Person for index information.
2016-09-20 14:56:57,452 DEBUG .data.mongodb.core.ReactiveMongoTemplate: 975 - Inserting Document containing fields: [_class, name, age] in collection: person
2016-09-20 14:56:57,541 DEBUG .data.mongodb.core.ReactiveMongoTemplate:1503 - findOne using query: { "name" : "Joe"} fields: null for class: class example.ReactiveMongoApp$Person in collection: person
2016-09-20 14:56:57,545 DEBUG .data.mongodb.core.ReactiveMongoTemplate:1979 - findOne using query: { "name" : "Joe"} in db.collection: database.person
2016-09-20 14:56:57,567 INFO example.ReactiveMongoApp: 43 - Person [id=57e1321977ac501c68d73104, name=Joe, age=34]
2016-09-20 14:56:57,573 DEBUG .data.mongodb.core.ReactiveMongoTemplate: 528 - Dropped collection [person]
Even in this simple example, there are a few things to take notice of:
-
You can instantiate the central helper class of Spring Mongo (
ReactiveMongoTemplate
) by using the standardcom.mongodb.reactivestreams.client.MongoClient
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 an
ObjectId
when stored in the database. -
Mapping conventions can use field access. Notice that the
Person
class has only getters. -
If the constructor argument names match the field names of the stored document, they are used to instantiate the object
There is a GitHub repository with several examples that you can download and play around with to get a feel for how the library works.
14.2. Connecting to MongoDB with Spring and the Reactive Streams Driver
One of the first tasks when using MongoDB and Spring is to create a com.mongodb.reactivestreams.client.MongoClient
object by using the IoC container.
14.2.1. Registering a MongoClient Instance Using Java-based Metadata
The following example shows how to use Java-based bean metadata to register an instance of a com.mongodb.reactivestreams.client.MongoClient
:
com.mongodb.reactivestreams.client.MongoClient
object using Java based bean metadata@Configuration
public class AppConfig {
/*
* Use the Reactive Streams Mongo Client API to create a com.mongodb.reactivestreams.client.MongoClient instance.
*/
public @Bean MongoClient reactiveMongoClient() {
return MongoClients.create("mongodb://localhost");
}
}
This approach lets you use the standard com.mongodb.reactivestreams.client.MongoClient
API (which you may already know).
An alternative is to register an instance of com.mongodb.reactivestreams.client.MongoClient
instance with the container by using Spring’s ReactiveMongoClientFactoryBean
. As compared to instantiating a com.mongodb.reactivestreams.client.MongoClient
instance directly, the FactoryBean
approach has the added advantage of also providing the container with an ExceptionTranslator
implementation that translates MongoDB exceptions to exceptions in Spring’s portable DataAccessException
hierarchy for data access classes annotated with the @Repository
annotation. This hierarchy and use of @Repository
is described in Spring’s DAO support features.
The following example shows Java-based bean metadata that supports exception translation on @Repository
annotated classes:
com.mongodb.reactivestreams.client.MongoClient
object using Spring’s MongoClientFactoryBean and enabling Spring’s exception translation support@Configuration
public class AppConfig {
/*
* Factory bean that creates the com.mongodb.reactivestreams.client.MongoClient instance
*/
public @Bean ReactiveMongoClientFactoryBean mongoClient() {
ReactiveMongoClientFactoryBean clientFactory = new ReactiveMongoClientFactoryBean();
clientFactory.setHost("localhost");
return clientFactory;
}
}
To access the com.mongodb.reactivestreams.client.MongoClient
object created by the ReactiveMongoClientFactoryBean
in other @Configuration
or your own classes, get the MongoClient
from the context.
14.2.2. The ReactiveMongoDatabaseFactory Interface
While com.mongodb.reactivestreams.client.MongoClient
is the entry point to the reactive MongoDB driver API, connecting to a specific MongoDB database instance requires additional information, such as the database name. With that information, you can obtain a com.mongodb.reactivestreams.client.MongoDatabase
object and access all the functionality of a specific MongoDB database instance. Spring provides the org.springframework.data.mongodb.core.ReactiveMongoDatabaseFactory
interface to bootstrap connectivity to the database. The following listing shows the ReactiveMongoDatabaseFactory
interface:
public interface ReactiveMongoDatabaseFactory {
/**
* Creates a default {@link MongoDatabase} instance.
*
* @return
* @throws DataAccessException
*/
MongoDatabase getMongoDatabase() throws DataAccessException;
/**
* Creates a {@link MongoDatabase} instance to access the database with the given name.
*
* @param dbName must not be {@literal null} or empty.
* @return
* @throws DataAccessException
*/
MongoDatabase getMongoDatabase(String dbName) throws DataAccessException;
/**
* Exposes a shared {@link MongoExceptionTranslator}.
*
* @return will never be {@literal null}.
*/
PersistenceExceptionTranslator getExceptionTranslator();
}
The org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory
class implements the ReactiveMongoDatabaseFactory
interface and is created with a standard com.mongodb.reactivestreams.client.MongoClient
instance and the database name.
Instead of using the IoC container to create an instance of ReactiveMongoTemplate
, you can use them in standard Java code, as follows:
public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class);
public static void main(String[] args) throws Exception {
ReactiveMongoOperations mongoOps = new ReactiveMongoOperations(new SimpleReactiveMongoDatabaseFactory(MongoClient.create(), "database"));
mongoOps.insert(new Person("Joe", 34))
.flatMap(p -> mongoOps.findOne(new Query(where("name").is("Joe")), Person.class))
.doOnNext(person -> log.info(person.toString()))
.flatMap(person -> mongoOps.dropCollection("person"))
.subscribe();
}
}
The use of SimpleReactiveMongoDatabaseFactory
is the only difference between the listing shown in the getting started section.
14.2.3. Registering a ReactiveMongoDatabaseFactory Instance by Using Java-based Metadata
To register a ReactiveMongoDatabaseFactory
instance with the container, you can write code much like what was highlighted in the previous code listing, as the following example shows:
@Configuration
public class MongoConfiguration {
public @Bean ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory() {
return new SimpleReactiveMongoDatabaseFactory(MongoClients.create(), "database");
}
}
To define the username and password, create a MongoDB connection string and pass it into the factory method, as the next listing shows. The following listing also shows how to use ReactiveMongoDatabaseFactory
to register an instance of ReactiveMongoTemplate
with the container:
@Configuration
public class MongoConfiguration {
public @Bean ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory() {
return new SimpleReactiveMongoDatabaseFactory(MongoClients.create("mongodb://joe:secret@localhost"), "database");
}
public @Bean ReactiveMongoTemplate reactiveMongoTemplate() {
return new ReactiveMongoTemplate(reactiveMongoDatabaseFactory());
}
}
14.3. Introduction to ReactiveMongoTemplate
The ReactiveMongoTemplate
class, located in the org.springframework.data.mongodb
package, is the central class of the Spring’s Reactive MongoDB support and provides a rich feature set to interact with the database. The template offers convenience operations to create, update, delete, and query for MongoDB documents and provides a mapping between your domain objects and MongoDB documents.
Once configured, ReactiveMongoTemplate 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 MongoConverter
interface. Spring provides a default implementation with MongoMappingConverter
, but you can also write your own converter. See the section on MongoConverter
instances for more detailed information.
The ReactiveMongoTemplate
class implements the ReactiveMongoOperations
interface. As much as possible, the methods on ReactiveMongoOperations
mirror methods available on the MongoDB driver Collection
object, to make the API familiar to existing MongoDB developers who are used to the driver API. For example, you can find methods such as find
, findAndModify
, findOne
, insert
, remove
, save
, update
, and updateMulti
. The design goal is to make it as easy as possible to transition between the use of the base MongoDB driver and ReactiveMongoOperations
. A major difference between the two APIs is that ReactiveMongoOperations
can be passed domain objects instead of Document
, and there are fluent APIs for Query
, Criteria
, and Update
operations instead of populating a Document
to specify the parameters for those operations.
The preferred way to reference the operations on ReactiveMongoTemplate instance is through its ReactiveMongoOperations interface.
|
The default converter implementation used by ReactiveMongoTemplate
is MappingMongoConverter
. While the MappingMongoConverter
can use additional metadata to specify the mapping of objects to documents, it can also convert objects that contain no additional metadata by using some conventions for the mapping of IDs and collection names. These conventions as well as the use of mapping annotations are explained in the Mapping chapter.
Another central feature of ReactiveMongoTemplate
is exception translation of exceptions thrown in the MongoDB Java driver into Spring’s portable Data Access Exception hierarchy. See the section on exception translation for more information.
There are many convenience methods on ReactiveMongoTemplate
to help you easily perform common tasks. However, if you 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 give you a reference to either a com.mongodb.reactivestreams.client.MongoCollection
or a com.mongodb.reactivestreams.client.MongoDatabase
object. See Execution Callbacks for more information.
14.3.1. Instantiating ReactiveMongoTemplate
You can use Java to create and register an instance of ReactiveMongoTemplate
, as follows:
com.mongodb.reactivestreams.client.MongoClient
object and enabling Spring’s exception translation support@Configuration
public class AppConfig {
public @Bean MongoClient reactiveMongoClient() {
return MongoClients.create("mongodb://localhost");
}
public @Bean ReactiveMongoTemplate reactiveMongoTemplate() {
return new ReactiveMongoTemplate(reactiveMongoClient(), "mydatabase");
}
}
There are several overloaded constructors of ReactiveMongoTemplate
, including:
-
ReactiveMongoTemplate(MongoClient mongo, String databaseName)
: Takes thecom.mongodb.reactivestreams.client.MongoClient
object and the default database name to operate against. -
ReactiveMongoTemplate(ReactiveMongoDatabaseFactory mongoDatabaseFactory)
: Takes aReactiveMongoDatabaseFactory
object that encapsulated thecom.mongodb.reactivestreams.client.MongoClient
object and database name. -
ReactiveMongoTemplate(ReactiveMongoDatabaseFactory mongoDatabaseFactory, MongoConverter mongoConverter)
: Adds aMongoConverter
to use for mapping.
When creating a ReactiveMongoTemplate
, you might also want to set the following properties:
-
WriteResultCheckingPolicy
-
WriteConcern
-
ReadPreference
The preferred way to reference the operations on ReactiveMongoTemplate instance is through its ReactiveMongoOperations interface.
|
14.3.2. WriteResultChecking
Policy
When in development, it is handy to either log or throw an Exception
if the com.mongodb.WriteResult
returned from any MongoDB operation contains an error. It is quite common to forget to do this during development and then end up with an application that looks like it runs successfully when, in fact, the database was not modified according to your expectations. Set the MongoTemplate
WriteResultChecking
property to an enum with the following values, LOG
, EXCEPTION
, or NONE
to either log the error, throw and exception or do nothing. The default is to use a WriteResultChecking
value of NONE
.
14.3.3. WriteConcern
If it has not yet been specified through the driver at a higher level (such as MongoDatabase
), you can set the com.mongodb.WriteConcern
property that the ReactiveMongoTemplate
uses for write operations. If ReactiveMongoTemplate’s WriteConcern
property is not set, it defaults to the one set in the MongoDB driver’s MongoDatabase
or MongoCollection
setting.
14.3.4. 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 ReactiveMongoTemplate
. Since ReactiveMongoTemplate
is used to persist POJOs, the WriteConcernResolver
lets you create a policy that can map a specific POJO class to a WriteConcern
value. The following listing shows the WriteConcernResolver
interface:
public interface WriteConcernResolver {
WriteConcern resolve(MongoAction action);
}
The argument, MongoAction
, determines the WriteConcern
value to be used and whether 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
, the operation as a value from the MongoActionOperation
enumeration (one of REMOVE
, UPDATE
, INSERT
, INSERT_LIST
, and SAVE
), and a few other pieces of contextual information. The following example shows how to create a WriteConcernResolver
:
private class MyAppWriteConcernResolver implements WriteConcernResolver {
public WriteConcern resolve(MongoAction action) {
if (action.getEntityClass().getSimpleName().contains("Audit")) {
return WriteConcern.NONE;
} else if (action.getEntityClass().getSimpleName().contains("Metadata")) {
return WriteConcern.JOURNAL_SAFE;
}
return action.getDefaultWriteConcern();
}
}
14.4. Saving, Updating, and Removing Documents
ReactiveMongoTemplate
lets you save, update, and delete your domain objects and map those objects to documents stored in MongoDB.
Consider the following Person
class:
public class Person {
private String id;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
The following listing shows how you can save, update, and delete the Person
object:
public class ReactiveMongoApp {
private static final Logger log = LoggerFactory.getLogger(ReactiveMongoApp.class);
public static void main(String[] args) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
ReactiveMongoTemplate mongoOps = new ReactiveMongoTemplate(MongoClients.create(), "database");
mongoOps.insert(new Person("Joe", 34)).doOnNext(person -> log.info("Insert: " + person))
.flatMap(person -> mongoOps.findById(person.getId(), Person.class))
.doOnNext(person -> log.info("Found: " + person))
.zipWith(person -> mongoOps.updateFirst(query(where("name").is("Joe")), update("age", 35), Person.class))
.flatMap(tuple -> mongoOps.remove(tuple.getT1())).flatMap(deleteResult -> mongoOps.findAll(Person.class))
.count().doOnSuccess(count -> {
log.info("Number of people: " + count);
latch.countDown();
})
.subscribe();
latch.await();
}
}
The preceding example includes implicit conversion between a String
and ObjectId
(by using the MongoConverter
) as stored in the database and recognizing a convention of the property Id
name.
The preceding example is meant to show the use of save, update, and remove operations on ReactiveMongoTemplate and not to show complex mapping or chaining functionality.
|
“Querying Documents” explains the query syntax used in the preceding example in more detail. Additional documentation can be found in the blocking MongoTemplate
section.
14.5. Execution Callbacks
One common design feature of all Spring template classes is that all functionality is routed into one of the templates that run callback methods. This helps ensure that exceptions and any resource management that maybe required are performed consistency. While this was of much greater need in the case of JDBC and JMS than with MongoDB, it still offers a single spot for exception translation and logging to occur. As such, using the execute
callback is the preferred way to access the MongoDB driver’s MongoDatabase
and MongoCollection
objects to perform uncommon operations that were not exposed as methods on ReactiveMongoTemplate
.
Here is a list of execute
callback methods.
-
<T> Flux<T>
execute(Class<?> entityClass, ReactiveCollectionCallback<T> action)
: Runs the givenReactiveCollectionCallback
for the entity collection of the specified class. -
<T> Flux<T>
execute(String collectionName, ReactiveCollectionCallback<T> action)
: Runs the givenReactiveCollectionCallback
on the collection of the given name. -
<T> Flux<T>
execute(ReactiveDatabaseCallback<T> action)
: Runs aReactiveDatabaseCallback
translating any exceptions as necessary.
The following example uses the ReactiveCollectionCallback
to return information about an index:
Flux<Boolean> hasIndex = operations.execute("geolocation",
collection -> Flux.from(collection.listIndexes(Document.class))
.filter(document -> document.get("name").equals("fancy-index-name"))
.flatMap(document -> Mono.just(true))
.defaultIfEmpty(false));
14.6. GridFS Support
MongoDB supports storing binary files inside its filesystem, GridFS.
Spring Data MongoDB provides a ReactiveGridFsOperations
interface as well as the corresponding implementation, ReactiveGridFsTemplate
, to let you interact with the filesystem.
You can set up a ReactiveGridFsTemplate
instance by handing it a ReactiveMongoDatabaseFactory
as well as a MongoConverter
, as the following example shows:
class GridFsConfiguration extends AbstractReactiveMongoConfiguration {
// … further configuration omitted
@Bean
public ReactiveGridFsTemplate reactiveGridFsTemplate() {
return new ReactiveGridFsTemplate(reactiveMongoDbFactory(), mappingMongoConverter());
}
}
The template can now be injected and used to perform storage and retrieval operations, as the following example shows:
class ReactiveGridFsClient {
@Autowired
ReactiveGridFsTemplate operations;
@Test
public Mono<ObjectId> storeFileToGridFs() {
FileMetadata metadata = new FileMetadata();
// populate metadata
Publisher<DataBuffer> file = … // lookup File or Resource
return operations.store(file, "filename.txt", metadata);
}
}
The store(…)
operations take an Publisher<DataBuffer>
, a filename, and (optionally) metadata information about the file to store. The metadata can be an arbitrary object, which will be marshaled by the MongoConverter
configured with the ReactiveGridFsTemplate
. Alternatively, you can also provide a Document
.
MongoDB’s driver uses AsyncInputStream and AsyncOutputStream interfaces to exchange binary streams. Spring Data MongoDB adapts these interfaces to Publisher<DataBuffer> . Read more about DataBuffer in Spring’s reference documentation.
|
You can read files from the filesystem through either the find(…)
or the getResources(…)
methods. Let’s have a look at the find(…)
methods first. You can either find a single file or multiple files that match a Query
. You can use the GridFsCriteria
helper class to define queries. It provides static factory methods to encapsulate default metadata fields (such as whereFilename()
and whereContentType()
) or a custom one through whereMetaData()
. The following example shows how to use ReactiveGridFsTemplate
to query for files:
class ReactiveGridFsClient {
@Autowired
ReactiveGridFsTemplate operations;
@Test
public Flux<GridFSFile> findFilesInGridFs() {
return operations.find(query(whereFilename().is("filename.txt")))
}
}
Currently, MongoDB does not support defining sort criteria when retrieving files from GridFS. For this reason, any sort criteria defined on the Query instance handed into the find(…) method are disregarded.
|
The other option to read files from the GridFs is to use the methods modeled along the lines of ResourcePatternResolver
.
ReactiveGridFsOperations
uses reactive types to defer running while ResourcePatternResolver
uses a synchronous interface.
These methods allow handing an Ant path into the method and can thus retrieve files matching the given pattern. The following example shows how to use ReactiveGridFsTemplate
to read files:
class ReactiveGridFsClient {
@Autowired
ReactiveGridFsOperations operations;
@Test
public void readFilesFromGridFs() {
Flux<ReactiveGridFsResource> txtFiles = operations.getResources("*.txt");
}
}
15. MongoDB Repositories
15.1. Introduction
This chapter points out the specialties for repository support for MongoDB. This chapter builds on the core repository support explained in Working with Spring Data Repositories. You should have a sound understanding of the basic concepts explained there.
15.2. Usage
To access domain entities stored in a MongoDB, you can use our sophisticated repository support that eases implementation quite significantly. To do so, create an interface for your repository, as the following example shows:
public class Person {
@Id
private String id;
private String firstname;
private String lastname;
private Address address;
// … getters and setters omitted
}
Note that the domain type shown in the preceding example has a property named id
of type String
.The default serialization mechanism used in MongoTemplate
(which backs the repository support) regards properties named id
as the document ID.
Currently, we support String
, ObjectId
, and BigInteger
as ID types.
Please see ID mapping for more information about on how the id
field is handled in the mapping layer.
Now that we have a domain object, we can define an interface that uses it, as follows:
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
// additional custom query methods go here
}
Right now this interface serves only to provide type information, but we can add additional methods to it later.
To start using the repository, use the @EnableMongoRepositories
annotation.
That annotation carries the same attributes as the namespace element.
If no base package is configured, the infrastructure scans the package of the annotated configuration class.
The following example shows how to use Java configuration for a repository:
@Configuration
@EnableMongoRepositories
class ApplicationConfig extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "e-store";
}
@Override
protected String getMappingBasePackage() {
return "com.oreilly.springdata.mongodb";
}
}
If you would rather go with XML based configuration add the following content:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
https://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<mongo:mongo-client id="mongoClient" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoClient" />
<constructor-arg value="databaseName" />
</bean>
<mongo:repositories base-package="com.acme.*.repositories" />
</beans>
This namespace element causes the base packages to be scanned for interfaces that extend MongoRepository
and create Spring beans for each one found.
By default, the repositories 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.
Because our domain repository extends PagingAndSortingRepository
, it provides you with CRUD operations as well as methods for paginated and sorted access to the entities.
Working with the repository instance is just a matter of dependency injecting it into a client .
Consequently, accessing the second page of Person
objects at a page size of 10 would resemble the following code:
@RunWith(SpringRunner.class)
@ContextConfiguration
public class PersonRepositoryTests {
@Autowired PersonRepository repository;
@Test
public void readsFirstPageCorrectly() {
Page<Person> persons = repository.findAll(PageRequest.of(0, 10));
assertThat(persons.isFirstPage()).isTrue();
}
}
The preceding example creates an application context with Spring’s unit test support, which performs annotation-based dependency injection into test cases.
Inside the test method, we use the repository to query the datastore.
We hand the repository a PageRequest
instance that requests the first page of Person
objects at a page size of 10.
15.3. Query Methods
Most of the data access operations you usually trigger on a repository result in a query being executed against the MongoDB databases. Defining such a query is a matter of declaring a method on the repository interface, as the following example shows:
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
List<Person> findByLastname(String lastname); (1)
Page<Person> findByFirstname(String firstname, Pageable pageable); (2)
Person findByShippingAddresses(Address address); (3)
Person findFirstByLastname(String lastname) (4)
Stream<Person> findAllBy(); (5)
}
1 | The findByLastname method shows a query for all people with the given last name.
The query is derived by parsing the method name for constraints that can be concatenated with And and Or .
Thus, the method name results in a query expression of {"lastname" : lastname} . |
2 | Applies pagination to a query.
You can equip your method signature with a Pageable parameter and let the method return a Page instance and Spring Data automatically pages the query accordingly. |
3 | Shows that you can query based on properties that are not primitive types.
Throws IncorrectResultSizeDataAccessException if more than one match is found. |
4 | Uses the First keyword to restrict the query to only the first result.
Unlike <3>, this method does not throw an exception if more than one match is found. |
5 | Uses a Java 8 Stream that reads and converts individual elements while iterating the stream. |
We do not support referring to parameters that are mapped as DBRef in the domain class.
|
The following table shows the keywords that are supported for query methods:
Keyword | Sample | Logical result |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If the property criterion compares a document, the order of the fields and exact equality in the document matters. |
15.3.1. Repository Delete Queries
The keywords in the preceding table can be used in conjunction with delete…By
or remove…By
to create queries that delete matching documents.
Delete…By
Querypublic interface PersonRepository extends MongoRepository<Person, String> {
List <Person> deleteByLastname(String lastname); (1)
Long deletePersonByLastname(String lastname); (2)
@Nullable
Person deleteSingleByLastname(String lastname); (3)
Optional<Person> deleteByBirthdate(Date birthdate); (4)
}
1 | Using a return type of List retrieves and returns all matching documents before actually deleting them. |
2 | A numeric return type directly removes the matching documents, returning the total number of documents removed. |
3 | A single domain type result retrieves and removes the first matching document. |
4 | Same as in 3 but wrapped in an Optional type. |
15.3.2. Geo-spatial Repository Queries
As you saw in the preceding table of keywords, a few keywords trigger geo-spatial operations within a MongoDB query.
The Near
keyword allows some further modification, as the next few examples show.
The following example shows how to define a near
query that finds all persons with a given distance of a given point:
Near
queriespublic interface PersonRepository extends MongoRepository<Person, String> {
// { 'location' : { '$near' : [point.x, point.y], '$maxDistance' : distance}}
List<Person> findByLocationNear(Point location, Distance distance);
}
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 transparently use $nearSphere
instead of $code
, as the following example shows:
Distance
with Metrics
Point point = new Point(43.7, 48.8);
Distance distance = new Distance(200, Metrics.KILOMETERS);
… = repository.findByLocationNear(point, distance);
// {'location' : {'$nearSphere' : [43.7, 48.8], '$maxDistance' : 0.03135711885774796}}
Using a Distance
with a Metric
causes a $nearSphere
(instead of a plain $near
) clause to be added.
Beyond that, the actual distance gets calculated according to the Metrics
used.
(Note that Metric
does not refer to metric units of measure.
It could be miles rather than kilometers.
Rather, metric
refers to the concept of a system of measurement, regardless of which system you use.)
Using @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) on the target property forces usage of the $nearSphere operator.
|
Geo-near Queries
Spring Data MongoDb supports geo-near queries, as the following example shows:
public interface PersonRepository extends MongoRepository<Person, String> {
// {'geoNear' : 'location', 'near' : [x, y] }
GeoResults<Person> findByLocationNear(Point location);
// No metric: {'geoNear' : 'person', 'near' : [x, y], maxDistance : distance }
// Metric: {'geoNear' : 'person', 'near' : [x, y], 'maxDistance' : distance,
// 'distanceMultiplier' : metric.multiplier, 'spherical' : true }
GeoResults<Person> findByLocationNear(Point location, Distance distance);
// Metric: {'geoNear' : 'person', 'near' : [x, y], 'minDistance' : min,
// 'maxDistance' : max, 'distanceMultiplier' : metric.multiplier,
// 'spherical' : true }
GeoResults<Person> findByLocationNear(Point location, Distance min, Distance max);
// {'geoNear' : 'location', 'near' : [x, y] }
GeoResults<Person> findByLocationNear(Point location);
}
15.3.3. MongoDB JSON-based Query Methods and Field Restriction
By adding the org.springframework.data.mongodb.repository.Query
annotation to your repository query methods, you can specify a MongoDB JSON query string to use instead of having the query be derived from the method name, as the following example shows:
public interface PersonRepository extends MongoRepository<Person, String> {
@Query("{ 'firstname' : ?0 }")
List<Person> findByThePersonsFirstname(String firstname);
}
The ?0
placeholder lets you substitute the value from the method arguments into the JSON query string.
String parameter values are escaped during the binding process, which means that it is not possible to add MongoDB specific operators through the argument.
|
You can also use the filter property to restrict the set of properties that is mapped into the Java object, as the following example shows:
public interface PersonRepository extends MongoRepository<Person, String> {
@Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
List<Person> findByThePersonsFirstname(String firstname);
}
The query in the preceding example returns only the firstname
, lastname
and Id
properties of the Person
objects.
The age
property, a java.lang.Integer
, is not set and its value is therefore null.
15.3.4. Sorting Query Method results
MongoDB repositories allow various approaches to define sorting order. Let’s take a look at the following example:
public interface PersonRepository extends MongoRepository<Person, String> {
List<Person> findByFirstnameSortByAgeDesc(String firstname); (1)
List<Person> findByFirstname(String firstname, Sort sort); (2)
@Query(sort = "{ age : -1 }")
List<Person> findByFirstname(String firstname); (3)
@Query(sort = "{ age : -1 }")
List<Person> findByLastname(String lastname, Sort sort); (4)
}
1 | Static sorting derived from method name. SortByAgeDesc results in { age : -1 } for the sort parameter. |
2 | Dynamic sorting using a method argument.
Sort.by(DESC, "age") creates { age : -1 } for the sort parameter. |
3 | Static sorting via Query annotation.
Sort parameter applied as stated in the sort attribute. |
4 | Default sorting via Query annotation combined with dynamic one via a method argument. Sort.unsorted()
results in { age : -1 } .
Using Sort.by(ASC, "age") overrides the defaults and creates { age : 1 } .
Sort.by
(ASC, "firstname") alters the default and results in { age : -1, firstname : 1 } . |
15.3.5. JSON-based Queries with SpEL Expressions
Query strings and field definitions can be used together with SpEL expressions to create dynamic queries at runtime. SpEL expressions can provide predicate values and can be used to extend predicates with subdocuments.
Expressions expose method arguments through an array that contains all the arguments.
The following query uses [0]
to declare the predicate value for lastname
(which is equivalent to the ?0
parameter binding):
public interface PersonRepository extends MongoRepository<Person, String> {
@Query("{'lastname': ?#{[0]} }")
List<Person> findByQueryWithExpression(String param0);
}
Expressions can be used to invoke functions, evaluate conditionals, and construct values. SpEL expressions used in conjunction with JSON reveal a side-effect, because Map-like declarations inside of SpEL read like JSON, as the following example shows:
public interface PersonRepository extends MongoRepository<Person, String> {
@Query("{'id': ?#{ [0] ? {$exists :true} : [1] }}")
List<Person> findByQueryWithExpressionAndNestedObject(boolean param0, String param1);
}
SpEL in query strings can be a powerful way to enhance queries. However, they can also accept a broad range of unwanted arguments. Make sure to sanitize strings before passing them to the query to avoid creation of vulnerabilities or unwanted changes to your query. |
Expression support is extensible through the Query SPI: org.springframework.data.repository.query.spi.EvaluationContextExtension
.
The Query SPI can contribute properties and functions and can customize the root object.
Extensions are retrieved from the application context at the time of SpEL evaluation when the query is built.
The following example shows how to use EvaluationContextExtension
:
public class SampleEvaluationContextExtension extends EvaluationContextExtensionSupport {
@Override
public String getExtensionId() {
return "security";
}
@Override
public Map<String, Object> getProperties() {
return Collections.singletonMap("principal", SecurityContextHolder.getCurrent().getPrincipal());
}
}
Bootstrapping MongoRepositoryFactory yourself is not application context-aware and requires further configuration to pick up Query SPI extensions.
|
Reactive query methods can make use of org.springframework.data.spel.spi.ReactiveEvaluationContextExtension .
|
15.3.6. Type-safe Query Methods
MongoDB repository support integrates with the Querydsl project, which provides a way to perform type-safe queries. To quote from the project description, "Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API." It provides the following features:
-
Code completion in the IDE (all properties, methods, and operations can be expanded in your favorite Java IDE).
-
Almost no syntactically invalid queries allowed (type-safe on all levels).
-
Domain types and properties can be referenced safely — no strings involved!
-
Adapts better to refactoring changes in domain types.
-
Incremental query definition is easier.
See the QueryDSL documentation for how to bootstrap your environment for APT-based code generation using Maven or Ant.
QueryDSL lets you write queries such as the following:
QPerson person = new QPerson("person");
List<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
Page<Person> page = repository.findAll(person.lastname.contains("a"),
PageRequest.of(0, 2, Direction.ASC, "lastname"));
QPerson
is a class that is generated by the Java annotation post-processing tool.
It is a Predicate
that lets you write type-safe queries.
Notice that there are no strings in the query other than the C0123
value.
You can use the generated Predicate
class by using the QuerydslPredicateExecutor
interface, which the following listing shows:
public interface QuerydslPredicateExecutor<T> {
T findOne(Predicate predicate);
List<T> findAll(Predicate predicate);
List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
Page<T> findAll(Predicate predicate, Pageable pageable);
Long count(Predicate predicate);
}
To use this in your repository implementation, add it to the list of repository interfaces from which your interface inherits, as the following example shows:
public interface PersonRepository extends MongoRepository<Person, String>, QuerydslPredicateExecutor<Person> {
// additional query methods go here
}
15.3.7. Full-text Search Queries
MongoDB’s full-text search feature is store-specific and, therefore, can be found on MongoRepository
rather than on the more general CrudRepository
.
We need a document with a full-text index (see “Text Indexes” to learn how to create a full-text index).
Additional methods on MongoRepository
take TextCriteria
as an input parameter.
In addition to those explicit methods, it is also possible to add a TextCriteria
-derived repository method.
The criteria are added as an additional AND
criteria.
Once the entity contains a @TextScore
-annotated property, the document’s full-text score can be retrieved.
Furthermore, the @TextScore
annotated also makes it possible to sort by the document’s score, as the following example shows:
@Document
class FullTextDocument {
@Id String id;
@TextIndexed String title;
@TextIndexed String content;
@TextScore Float score;
}
interface FullTextRepository extends Repository<FullTextDocument, String> {
// Execute a full-text search and define sorting dynamically
List<FullTextDocument> findAllBy(TextCriteria criteria, Sort sort);
// Paginate over a full-text search result
Page<FullTextDocument> findAllBy(TextCriteria criteria, Pageable pageable);
// Combine a derived query with a full-text search
List<FullTextDocument> findByTitleOrderByScoreDesc(String title, TextCriteria criteria);
}
Sort sort = Sort.by("score");
TextCriteria criteria = TextCriteria.forDefaultLanguage().matchingAny("spring", "data");
List<FullTextDocument> result = repository.findAllBy(criteria, sort);
criteria = TextCriteria.forDefaultLanguage().matching("film");
Page<FullTextDocument> page = repository.findAllBy(criteria, PageRequest.of(1, 1, sort));
List<FullTextDocument> result = repository.findByTitleOrderByScoreDesc("mongodb", criteria);
15.3.8. Projections
Spring Data query methods usually return one or multiple instances of the aggregate root managed by the repository. However, it might sometimes be desirable to create projections based on certain attributes of those types. Spring Data allows modeling dedicated return types, to more selectively retrieve partial views of the managed aggregates.
Imagine a repository and aggregate root type such as the following example:
class Person {
@Id UUID id;
String firstname, lastname;
Address address;
static class Address {
String zipCode, city, street;
}
}
interface PersonRepository extends Repository<Person, UUID> {
Collection<Person> findByLastname(String lastname);
}
Now imagine that we want to retrieve the person’s name attributes only. What means does Spring Data offer to achieve this? The rest of this chapter answers that question.
Interface-based Projections
The easiest way to limit the result of the queries to only the name attributes is by declaring an interface that exposes accessor methods for the properties to be read, as shown in the following example:
interface NamesOnly {
String getFirstname();
String getLastname();
}
The important bit here is that the properties defined here exactly match properties in the aggregate root. Doing so lets a query method be added as follows:
interface PersonRepository extends Repository<Person, UUID> {
Collection<NamesOnly> findByLastname(String lastname);
}
The query execution engine creates proxy instances of that interface at runtime for each element returned and forwards calls to the exposed methods to the target object.
Declaring a method in your Repository that overrides a base method (e.g. declared in CrudRepository , a store-specific repository interface, or the Simple…Repository ) results in a call to the base method regardless of the declared return type. Make sure to use a compatible return type as base methods cannot be used for projections. Some store modules support @Query annotations to turn an overridden base method into a query method that then can be used to return projections.
|
Projections can be used recursively. If you want to include some of the Address
information as well, create a projection interface for that and return that interface from the declaration of getAddress()
, as shown in the following example:
interface PersonSummary {
String getFirstname();
String getLastname();
AddressSummary getAddress();
interface AddressSummary {
String getCity();
}
}
On method invocation, the address
property of the target instance is obtained and wrapped into a projecting proxy in turn.
Closed Projections
A projection interface whose accessor methods all match properties of the target aggregate is considered to be a closed projection. The following example (which we used earlier in this chapter, too) is a closed projection:
interface NamesOnly {
String getFirstname();
String getLastname();
}
If you use a closed projection, Spring Data can optimize the query execution, because we know about all the attributes that are needed to back the projection proxy. For more details on that, see the module-specific part of the reference documentation.
Open Projections
Accessor methods in projection interfaces can also be used to compute new values by using the @Value
annotation, as shown in the following example:
interface NamesOnly {
@Value("#{target.firstname + ' ' + target.lastname}")
String getFullName();
…
}
The aggregate root backing the projection is available in the target
variable.
A projection interface using @Value
is an open projection.
Spring Data cannot apply query execution optimizations in this case, because the SpEL expression could use any attribute of the aggregate root.
The expressions used in @Value
should not be too complex — you want to avoid programming in String
variables.
For very simple expressions, one option might be to resort to default methods (introduced in Java 8), as shown in the following example:
interface NamesOnly {
String getFirstname();
String getLastname();
default String getFullName() {
return getFirstname().concat(" ").concat(getLastname());
}
}
This approach requires you to be able to implement logic purely based on the other accessor methods exposed on the projection interface. A second, more flexible, option is to implement the custom logic in a Spring bean and then invoke that from the SpEL expression, as shown in the following example:
@Component
class MyBean {
String getFullName(Person person) {
…
}
}
interface NamesOnly {
@Value("#{@myBean.getFullName(target)}")
String getFullName();
…
}
Notice how the SpEL expression refers to myBean
and invokes the getFullName(…)
method and forwards the projection target as a method parameter.
Methods backed by SpEL expression evaluation can also use method parameters, which can then be referred to from the expression.
The method parameters are available through an Object
array named args
. The following example shows how to get a method parameter from the args
array:
interface NamesOnly {
@Value("#{args[0] + ' ' + target.firstname + '!'}")
String getSalutation(String prefix);
}
Again, for more complex expressions, you should use a Spring bean and let the expression invoke a method, as described earlier.
Nullable Wrappers
Getters in projection interfaces can make use of nullable wrappers for improved null-safety. Currently supported wrapper types are:
-
java.util.Optional
-
com.google.common.base.Optional
-
scala.Option
-
io.vavr.control.Option
interface NamesOnly {
Optional<String> getFirstname();
}
If the underlying projection value is not null
, then values are returned using the present-representation of the wrapper type.
In case the backing value is null
, then the getter method returns the empty representation of the used wrapper type.
Class-based Projections (DTOs)
Another way of defining projections is by using value type DTOs (Data Transfer Objects) that hold properties for the fields that are supposed to be retrieved. These DTO types can be used in exactly the same way projection interfaces are used, except that no proxying happens and no nested projections can be applied.
If the store optimizes the query execution by limiting the fields to be loaded, the fields to be loaded are determined from the parameter names of the constructor that is exposed.
The following example shows a projecting DTO:
class NamesOnly {
private final String firstname, lastname;
NamesOnly(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
String getFirstname() {
return this.firstname;
}
String getLastname() {
return this.lastname;
}
// equals(…) and hashCode() implementations
}
Avoid boilerplate code for projection DTOs
You can dramatically simplify the code for a DTO by using Project Lombok, which provides an
Fields are |
Dynamic Projections
So far, we have used the projection type as the return type or element type of a collection. However, you might want to select the type to be used at invocation time (which makes it dynamic). To apply dynamic projections, use a query method such as the one shown in the following example:
interface PersonRepository extends Repository<Person, UUID> {
<T> Collection<T> findByLastname(String lastname, Class<T> type);
}
This way, the method can be used to obtain the aggregates as is or with a projection applied, as shown in the following example:
void someMethod(PersonRepository people) {
Collection<Person> aggregates =
people.findByLastname("Matthews", Person.class);
Collection<NamesOnly> aggregates =
people.findByLastname("Matthews", NamesOnly.class);
}
Query parameters of type Class are inspected whether they qualify as dynamic projection parameter.
If the actual return type of the query equals the generic parameter type of the Class parameter, then the matching Class parameter is not available for usage within the query or SpEL expressions.
If you want to use a Class parameter as query argument then make sure to use a different generic parameter, for example Class<?> .
|
15.3.9. Aggregation Repository Methods
The repository layer offers means to interact with the aggregation framework via annotated repository query methods.
Similar to the JSON based queries, you can define a pipeline using the org.springframework.data.mongodb.repository.Aggregation
annotation.
The definition may contain simple placeholders like ?0
as well as SpEL expressions ?#{ … }
.
public interface PersonRepository extends CrudReppsitory<Person, String> {
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
List<PersonAggregate> groupByLastnameAndFirstnames(); (1)
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
List<PersonAggregate> groupByLastnameAndFirstnames(Sort sort); (2)
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : ?0 } } }")
List<PersonAggregate> groupByLastnameAnd(String property); (3)
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : ?0 } } }")
Slice<PersonAggregate> groupByLastnameAnd(String property, Pageable page); (4)
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
Stream<PersonAggregate> groupByLastnameAndFirstnamesAsStream(); (5)
@Aggregation("{ $group : { _id : null, total : { $sum : $age } } }")
SumValue sumAgeUsingValueWrapper(); (6)
@Aggregation("{ $group : { _id : null, total : { $sum : $age } } }")
Long sumAge(); (7)
@Aggregation("{ $group : { _id : null, total : { $sum : $age } } }")
AggregationResults<SumValue> sumAgeRaw(); (8)
@Aggregation("{ '$project': { '_id' : '$lastname' } }")
List<String> findAllLastnames(); (9)
}
public class PersonAggregate {
private @Id String lastname; (2)
private List<String> names;
public PersonAggregate(String lastname, List<String> names) {
// ...
}
// Getter / Setter omitted
}
public class SumValue {
private final Long total; (6) (8)
public SumValue(Long total) {
// ...
}
// Getter omitted
}
1 | Aggregation pipeline to group first names by lastname in the Person collection returning these as PersonAggregate . |
2 | If Sort argument is present, $sort is appended after the declared pipeline stages so that it only affects the order of the final results after having passed all other aggregation stages.
Therefore, the Sort properties are mapped against the methods return type PersonAggregate which turns Sort.by("lastname") into { $sort : { '_id', 1 } } because PersonAggregate.lastname is annotated with @Id . |
3 | Replaces ?0 with the given value for property for a dynamic aggregation pipeline. |
4 | $skip , $limit and $sort can be passed on via a Pageable argument. Same as in <2>, the operators are appended to the pipeline definition. Methods accepting Pageable can return Slice for easier pagination. |
5 | Aggregation methods can return Stream to consume results directly from an underlying cursor. Make sure to close the stream after consuming it to release the server-side cursor by either calling close() or through try-with-resources . |
6 | Map the result of an aggregation returning a single Document to an instance of a desired SumValue target type. |
7 | Aggregations resulting in single document holding just an accumulation result like eg. $sum can be extracted directly from the result Document .
To gain more control, you might consider AggregationResult as method return type as shown in <7>. |
8 | Obtain the raw AggregationResults mapped to the generic target wrapper type SumValue or org.bson.Document . |
9 | Like in <6>, a single value can be directly obtained from multiple result Document s. |
In some scenarios, aggregations might require additional options, such as a maximum run time, additional log comments, or the permission to temporarily write data to disk.
Use the @Meta
annotation to set those options via maxExecutionTimeMs
, comment
or allowDiskUse
.
public interface PersonRepository extends CrudReppsitory<Person, String> {
@Meta(allowDiskUse = true)
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
List<PersonAggregate> groupByLastnameAndFirstnames();
}
Or use @Meta
to create your own annotation as shown in the sample below.
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
@Meta(allowDiskUse = true)
@interface AllowDiskUse { }
public interface PersonRepository extends CrudReppsitory<Person, String> {
@AllowDiskUse
@Aggregation("{ $group: { _id : $lastname, names : { $addToSet : $firstname } } }")
List<PersonAggregate> groupByLastnameAndFirstnames();
}
You can use @Aggregation also with Reactive Repositories.
|
Simple-type single-result inspects the returned
|
The Page return type is not supported for repository methods using @Aggregation . However, you can use a
Pageable argument to add $skip , $limit and $sort to the pipeline and let the method return Slice .
|
15.4. CDI Integration
Instances of the repository interfaces are usually created by a container, and 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 lets you use the repository abstraction in CDI environments.
The extension is part of the JAR.
To activate it, drop the Spring Data MongoDB JAR into your classpath.
You can now set up the infrastructure by implementing a CDI Producer for the MongoTemplate
, as the following example shows:
class MongoTemplateProducer {
@Produces
@ApplicationScoped
public MongoOperations createMongoTemplate() {
MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database");
return new MongoTemplate(factory);
}
}
The Spring Data MongoDB CDI extension picks up the MongoTemplate
available as a CDI bean and creates a proxy for a Spring Data repository whenever a 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, as the following example shows:
class RepositoryClient {
@Inject
PersonRepository repository;
public void businessMethod() {
List<Person> people = repository.findAll();
}
}
16. Reactive MongoDB repositories
This chapter describes the specialties for reactive repository support for MongoDB. This chapter builds on the core repository support explained in Working with Spring Data Repositories. You should have a sound understanding of the basic concepts explained there.
16.1. Reactive Composition Libraries
The reactive space offers various reactive composition libraries. The most common libraries are RxJava and Project Reactor.
Spring Data MongoDB is built on top of the MongoDB Reactive Streams driver, to provide maximal interoperability by relying on the Reactive Streams initiative. Static APIs, such as ReactiveMongoOperations
, are provided by using Project Reactor’s Flux
and Mono
types. Project Reactor offers various adapters to convert reactive wrapper types (Flux
to Observable
and vice versa), but conversion can easily clutter your code.
Spring Data’s Repository abstraction is a dynamic API, mostly defined by you and your requirements as you declare query methods. Reactive MongoDB repositories can be implemented by using either RxJava or Project Reactor wrapper types by extending from one of the following library-specific repository interfaces:
-
ReactiveCrudRepository
-
ReactiveSortingRepository
-
RxJava2CrudRepository
-
RxJava2SortingRepository
-
RxJava3CrudRepository
-
RxJava3SortingRepository
Spring Data converts reactive wrapper types behind the scenes so that you can stick to your favorite composition library.
16.2. Usage
To access domain entities stored in a MongoDB database, you can use our sophisticated repository support that eases implementing those quite significantly. To do so, create an interface similar for your repository. Before you can do that, though, you need an entity, such as the entity defined in the following example:
Person
entitypublic class Person {
@Id
private String id;
private String firstname;
private String lastname;
private Address address;
// … getters and setters omitted
}
Note that the entity defined in the preceding example has a property named id
of type String
. The default serialization mechanism used in MongoTemplate
(which backs the repository support) regards properties named id
as the document ID. Currently, we support String
, ObjectId
, and BigInteger
as id-types.
Please see ID mapping for more information about on how the id
field is handled in the mapping layer.
The following example shows how to create an interface that defines queries against the Person
object from the preceding example:
public interface ReactivePersonRepository extends ReactiveSortingRepository<Person, String> {
Flux<Person> findByFirstname(String firstname); (1)
Flux<Person> findByFirstname(Publisher<String> firstname); (2)
Flux<Person> findByFirstnameOrderByLastname(String firstname, Pageable pageable); (3)
Mono<Person> findByFirstnameAndLastname(String firstname, String lastname); (4)
Mono<Person> findFirstByLastname(String lastname); (5)
}
1 | The method shows a query for all people with the given lastname . The query is derived by parsing the method name for constraints that can be concatenated with And and Or . Thus, the method name results in a query expression of {"lastname" : lastname} . |
2 | The method shows a query for all people with the given firstname once the firstname is emitted by the given Publisher . |
3 | Use Pageable to pass offset and sorting parameters to the database. |
4 | Find a single entity for the given criteria. It completes with IncorrectResultSizeDataAccessException on non-unique results. |
5 | Unless <4>, the first entity is always emitted even if the query yields more result documents. |
For Java configuration, use the @EnableReactiveMongoRepositories
annotation. The annotation carries the same attributes as the namespace element. If no base package is configured, the infrastructure scans the package of the annotated configuration class.
MongoDB uses two different drivers for imperative (synchronous/blocking) and reactive (non-blocking) data access. You must create a connection by using the Reactive Streams driver to provide the required infrastructure for Spring Data’s Reactive MongoDB support. Consequently, you must provide a separate configuration for MongoDB’s Reactive Streams driver. Note that your application operates on two different connections if you use reactive and blocking Spring Data MongoDB templates and repositories. |
The following listing shows how to use Java configuration for a repository:
@Configuration
@EnableReactiveMongoRepositories
class ApplicationConfig extends AbstractReactiveMongoConfiguration {
@Override
protected String getDatabaseName() {
return "e-store";
}
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
}
@Override
protected String getMappingBasePackage() {
return "com.oreilly.springdata.mongodb";
}
}
Because our domain repository extends ReactiveSortingRepository
, it provides you with CRUD operations as well as methods for sorted access to the entities. Working with the repository instance is a matter of dependency injecting it into a client, as the following example shows:
public class PersonRepositoryTests {
@Autowired ReactivePersonRepository repository;
@Test
public void sortsElementsCorrectly() {
Flux<Person> persons = repository.findAll(Sort.by(new Order(ASC, "lastname")));
}
}
The Page return type (as in Mono<Page> ) is not supported by reactive repositories.
|
It is possible to use Pageable
in derived finder methods, to pass on sort
, limit
and offset
parameters to the query to reduce load and network traffic.
The returned Flux
will only emit data within the declared range.
Pageable page = PageRequest.of(1, 10, Sort.by("lastname"));
Flux<Person> persons = repository.findByFirstnameOrderByLastname("luke", page);
16.3. Features
Spring Data’s Reactive MongoDB support comes with a reduced feature set compared to the blocking MongoDB Repositories.
It supports the following features:
16.3.1. Geo-spatial Repository Queries
As you saw earlier in “Geo-spatial Repository Queries”, a few keywords trigger geo-spatial operations within a MongoDB query. The Near
keyword allows some further modification, as the next few examples show.
The following example shows how to define a near
query that finds all persons with a given distance of a given point:
Near
queriespublic interface PersonRepository extends ReactiveMongoRepository<Person, String> {
// { 'location' : { '$near' : [point.x, point.y], '$maxDistance' : distance}}
Flux<Person> findByLocationNear(Point location, Distance distance);
}
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 transparently use $nearSphere
instead of $code
, as the following example shows:
Distance
with Metrics
Point point = new Point(43.7, 48.8);
Distance distance = new Distance(200, Metrics.KILOMETERS);
… = repository.findByLocationNear(point, distance);
// {'location' : {'$nearSphere' : [43.7, 48.8], '$maxDistance' : 0.03135711885774796}}
Reactive Geo-spatial repository queries support the domain type and GeoResult<T> results within a reactive wrapper type. GeoPage and GeoResults are not supported as they contradict the deferred result approach with pre-calculating the average distance. Howevery, you can still pass in a Pageable argument to page results yourself.
|
Using a Distance
with a Metric
causes a $nearSphere
(instead of a plain $near
) clause to be added. Beyond that, the actual distance gets calculated according to the Metrics
used.
(Note that Metric
does not refer to metric units of measure. It could be miles rather than kilometers. Rather, metric
refers to the concept of a system of measurement, regardless of which system you use.)
Using @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) on the target property forces usage of $nearSphere operator.
|
Geo-near Queries
Spring Data MongoDB supports geo-near queries, as the following example shows:
public interface PersonRepository extends ReactiveMongoRepository<Person, String> {
// {'geoNear' : 'location', 'near' : [x, y] }
Flux<GeoResult<Person>> findByLocationNear(Point location);
// No metric: {'geoNear' : 'person', 'near' : [x, y], maxDistance : distance }
// Metric: {'geoNear' : 'person', 'near' : [x, y], 'maxDistance' : distance,
// 'distanceMultiplier' : metric.multiplier, 'spherical' : true }
Flux<GeoResult<Person>> findByLocationNear(Point location, Distance distance);
// Metric: {'geoNear' : 'person', 'near' : [x, y], 'minDistance' : min,
// 'maxDistance' : max, 'distanceMultiplier' : metric.multiplier,
// 'spherical' : true }
Flux<GeoResult<Person>> findByLocationNear(Point location, Distance min, Distance max);
// {'geoNear' : 'location', 'near' : [x, y] }
Flux<GeoResult<Person>> findByLocationNear(Point location);
}
16.3.2. Type-safe Query Methods
Reactive MongoDB repository support integrates with the Querydsl project, which provides a way to perform type-safe queries.
Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API.
It provides the following features:
-
Code completion in the IDE (all properties, methods, and operations can be expanded in your favorite Java IDE).
-
Almost no syntactically invalid queries allowed (type-safe on all levels).
-
Domain types and properties can be referenced safely — no strings involved!
-
Adapts better to refactoring changes in domain types.
-
Incremental query definition is easier.
See the Querydsl documentation for how to bootstrap your environment for APT-based code generation using Maven or Ant.
The Querydsl repository support lets you write and run queries, such as the following:
QPerson person = QPerson.person;
Flux<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
QPerson
is a class that is generated by the Java annotation post-processing tool. It is a Predicate
that lets you write type-safe queries.
Note that there are no strings in the query other than the C0123
value.
You can use the generated Predicate
class by using the ReactiveQuerydslPredicateExecutor
interface, which the following listing shows:
public interface ReactiveQuerydslPredicateExecutor<T> {
Mono<T> findOne(Predicate predicate);
Flux<T> findAll(Predicate predicate);
Flux<T> findAll(Predicate predicate, Sort sort);
Flux<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
Flux<T> findAll(OrderSpecifier<?>... orders);
Mono<Long> count(Predicate predicate);
Mono<Boolean> exists(Predicate predicate);
}
To use this in your repository implementation, add it to the list of repository interfaces from which your interface inherits, as the following example shows:
public interface PersonRepository extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {
// additional query methods go here
}
Please note that joins (DBRef’s) are not supported with Reactive MongoDB support. |
17. Auditing
17.1. Basics
Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and when the change happened. To benefit from that functionality, you have to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface. Additionally, auditing has to be enabled either through Annotation configuration or XML configuration to register the required infrastructure components. Please refer to the store-specific section for configuration samples.
Applications that only track creation and modification dates do not need to specify an |
17.1.1. Annotation-based Auditing Metadata
We provide @CreatedBy
and @LastModifiedBy
to capture the user who created or modified the entity as well as @CreatedDate
and @LastModifiedDate
to capture when the change happened.
class Customer {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
// … further properties omitted
}
As you can see, the annotations can be applied selectively, depending on which information you want to capture. The annotations capturing when changes were made can be used on properties of type Joda-Time, DateTime
, legacy Java Date
and Calendar
, JDK8 date and time types, and long
or Long
.
Auditing metadata does not necessarily need to live in the root level entity but can be added to an embedded one (depending on the actual store in use), as shown in the snipped below.
class Customer {
private AuditMetadata auditingMetadata;
// … further properties omitted
}
class AuditMetadata {
@CreatedBy
private User user;
@CreatedDate
private Instant createdDate;
}
17.1.2. Interface-based Auditing Metadata
In case you do not want to use annotations to define auditing metadata, you can let your domain class implement the Auditable
interface. It exposes setter methods for all of the auditing properties.
17.1.3. AuditorAware
In case you use either @CreatedBy
or @LastModifiedBy
, the auditing infrastructure somehow needs to become aware of the current principal. To do so, we provide an AuditorAware<T>
SPI interface that you have to implement to tell the infrastructure who the current user or system interacting with the application is. The generic type T
defines what type the properties annotated with @CreatedBy
or @LastModifiedBy
have to be.
The following example shows an implementation of the interface that uses Spring Security’s Authentication
object:
AuditorAware
based on Spring Securityclass SpringSecurityAuditorAware implements AuditorAware<User> {
@Override
public Optional<User> getCurrentAuditor() {
return Optional.ofNullable(SecurityContextHolder.getContext())
.map(SecurityContext::getAuthentication)
.filter(Authentication::isAuthenticated)
.map(Authentication::getPrincipal)
.map(User.class::cast);
}
}
The implementation accesses the Authentication
object provided by Spring Security and looks up the custom UserDetails
instance that you have created in your UserDetailsService
implementation. We assume here that you are exposing the domain user through the UserDetails
implementation but that, based on the Authentication
found, you could also look it up from anywhere.
17.1.4. ReactiveAuditorAware
When using reactive infrastructure you might want to make use of contextual information to provide @CreatedBy
or @LastModifiedBy
information.
We provide an ReactiveAuditorAware<T>
SPI interface that you have to implement to tell the infrastructure who the current user or system interacting with the application is. The generic type T
defines what type the properties annotated with @CreatedBy
or @LastModifiedBy
have to be.
The following example shows an implementation of the interface that uses reactive Spring Security’s Authentication
object:
ReactiveAuditorAware
based on Spring Securityclass SpringSecurityAuditorAware implements ReactiveAuditorAware<User> {
@Override
public Mono<User> getCurrentAuditor() {
return ReactiveSecurityContextHolder.getContext()
.map(SecurityContext::getAuthentication)
.filter(Authentication::isAuthenticated)
.map(Authentication::getPrincipal)
.map(User.class::cast);
}
}
The implementation accesses the Authentication
object provided by Spring Security and looks up the custom UserDetails
instance that you have created in your UserDetailsService
implementation. We assume here that you are exposing the domain user through the UserDetails
implementation but that, based on the Authentication
found, you could also look it up from anywhere.
17.2. General Auditing Configuration for MongoDB
Since Spring Data MongoDB 1.4, auditing can be enabled by annotating a configuration class with the @EnableMongoAuditing
annotation, as the followign example shows:
@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 picks it up automatically and uses 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 @EnableMongoAuditing
.
To activate auditing functionality via XML, add the Spring Data Mongo auditing
namespace element to your configuration, as the following example shows:
<mongo:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>
To enable auditing, leveraging a reactive programming model, use the @EnableReactiveMongoAuditing
annotation.
If you expose a bean of type ReactiveAuditorAware
to the ApplicationContext
, the auditing infrastructure picks it up automatically and uses 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 @EnableReactiveMongoAuditing
.
@Configuration
@EnableReactiveMongoAuditing
class Config {
@Bean
public ReactiveAuditorAware<AuditableUser> myAuditorProvider() {
return new AuditorAwareImpl();
}
}
18. Mapping
Rich mapping support is provided by the MappingMongoConverter
. MappingMongoConverter
has a rich metadata model that provides a full feature set to map domain objects to MongoDB documents.
The mapping metadata model is populated by using annotations on your domain objects.
However, the infrastructure is not limited to using annotations as the only source of metadata information.
The MappingMongoConverter
also lets you map objects to documents without providing any additional metadata, by following a set of conventions.
This section describes the features of the MappingMongoConverter
, including fundamentals, how to use conventions for mapping objects to documents and how to override those conventions with annotation-based mapping metadata.
18.1. Object Mapping Fundamentals
This section covers the fundamentals of Spring Data object mapping, object creation, field and property access, mutability and immutability. Note, that this section only applies to Spring Data modules that do not use the object mapping of the underlying data store (like JPA). Also be sure to consult the store-specific sections for store-specific object mapping, like indexes, customizing column or field names or the like.
Core responsibility of the Spring Data object mapping is to create instances of domain objects and map the store-native data structures onto those. This means we need two fundamental steps:
-
Instance creation by using one of the constructors exposed.
-
Instance population to materialize all exposed properties.
18.1.1. Object creation
Spring Data automatically tries to detect a persistent entity’s constructor to be used to materialize objects of that type. The resolution algorithm works as follows:
-
If there is a single constructor, it is used.
-
If there are multiple constructors and exactly one is annotated with
@PersistenceConstructor
, it is used. -
If there’s a no-argument constructor, it is used. Other constructors will be ignored.
The value resolution assumes constructor argument names to match the property names of the entity, i.e. the resolution will be performed as if the property was to be populated, including all customizations in mapping (different datastore column or field name etc.).
This also requires either parameter names information available in the class file or an @ConstructorProperties
annotation being present on the constructor.
The value resolution can be customized by using Spring Framework’s @Value
value annotation using a store-specific SpEL expression.
Please consult the section on store specific mappings for further details.
18.1.2. Property population
Once an instance of the entity has been created, Spring Data populates all remaining persistent properties of that class. Unless already populated by the entity’s constructor (i.e. consumed through its constructor argument list), the identifier property will be populated first to allow the resolution of cyclic object references. After that, all non-transient properties that have not already been populated by the constructor are set on the entity instance. For that we use the following algorithm:
-
If the property is immutable but exposes a
with…
method (see below), we use thewith…
method to create a new entity instance with the new property value. -
If property access (i.e. access through getters and setters) is defined, we’re invoking the setter method.
-
If the property is mutable we set the field directly.
-
If the property is immutable we’re using the constructor to be used by persistence operations (see Object creation) to create a copy of the instance.
-
By default, we set the field value directly.
Let’s have a look at the following entity:
class Person {
private final @Id Long id; (1)
private final String firstname, lastname; (2)
private final LocalDate birthday;
private final int age; (3)
private String comment; (4)
private @AccessType(Type.PROPERTY) String remarks; (5)
static Person of(String firstname, String lastname, LocalDate birthday) { (6)
return new Person(null, firstname, lastname, birthday,
Period.between(birthday, LocalDate.now()).getYears());
}
Person(Long id, String firstname, String lastname, LocalDate birthday, int age) { (6)
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.birthday = birthday;
this.age = age;
}
Person withId(Long id) { (1)
return new Person(id, this.firstname, this.lastname, this.birthday, this.age);
}
void setRemarks(String remarks) { (5)
this.remarks = remarks;
}
}
1 | The identifier property is final but set to null in the constructor.
The class exposes a withId(…) method that’s used to set the identifier, e.g. when an instance is inserted into the datastore and an identifier has been generated.
The original Person instance stays unchanged as a new one is created.
The same pattern is usually applied for other properties that are store managed but might have to be changed for persistence operations.
The wither method is optional as the persistence constructor (see 6) is effectively a copy constructor and setting the property will be translated into creating a fresh instance with the new identifier value applied. |
2 | The firstname and lastname properties are ordinary immutable properties potentially exposed through getters. |
3 | The age property is an immutable but derived one from the birthday property.
With the design shown, the database value will trump the defaulting as Spring Data uses the only declared constructor.
Even if the intent is that the calculation should be preferred, it’s important that this constructor also takes age as parameter (to potentially ignore it) as otherwise the property population step will attempt to set the age field and fail due to it being immutable and no with… method being present. |
4 | The comment property is mutable is populated by setting its field directly. |
5 | The remarks properties are mutable and populated by setting the comment field directly or by invoking the setter method for |
6 | The class exposes a factory method and a constructor for object creation.
The core idea here is to use factory methods instead of additional constructors to avoid the need for constructor disambiguation through @PersistenceConstructor .
Instead, defaulting of properties is handled within the factory method. |
18.1.3. General recommendations
-
Try to stick to immutable objects — Immutable objects are straightforward to create as materializing an object is then a matter of calling its constructor only. Also, this avoids your domain objects to be littered with setter methods that allow client code to manipulate the objects state. If you need those, prefer to make them package protected so that they can only be invoked by a limited amount of co-located types. Constructor-only materialization is up to 30% faster than properties population.
-
Provide an all-args constructor — Even if you cannot or don’t want to model your entities as immutable values, there’s still value in providing a constructor that takes all properties of the entity as arguments, including the mutable ones, as this allows the object mapping to skip the property population for optimal performance.
-
Use factory methods instead of overloaded constructors to avoid
@PersistenceConstructor
— With an all-argument constructor needed for optimal performance, we usually want to expose more application use case specific constructors that omit things like auto-generated identifiers etc. It’s an established pattern to rather use static factory methods to expose these variants of the all-args constructor. -
Make sure you adhere to the constraints that allow the generated instantiator and property accessor classes to be used —
-
For identifiers to be generated, still use a final field in combination with an all-arguments persistence constructor (preferred) or a
with…
method — -
Use Lombok to avoid boilerplate code — As persistence operations usually require a constructor taking all arguments, their declaration becomes a tedious repetition of boilerplate parameter to field assignments that can best be avoided by using Lombok’s
@AllArgsConstructor
.
Overriding Properties
Java’s allows a flexible design of domain classes where a subclass could define a property that is already declared with the same name in its superclass. Consider the following example:
public class SuperType {
private CharSequence field;
public SuperType(CharSequence field) {
this.field = field;
}
public CharSequence getField() {
return this.field;
}
public void setField(CharSequence field) {
this.field = field;
}
}
public class SubType extends SuperType {
private String field;
public SubType(String field) {
super(field);
this.field = field;
}
@Override
public String getField() {
return this.field;
}
public void setField(String field) {
this.field = field;
// optional
super.setField(field);
}
}
Both classes define a field
using assignable types. SubType
however shadows SuperType.field
.
Depending on the class design, using the constructor could be the only default approach to set SuperType.field
.
Alternatively, calling super.setField(…)
in the setter could set the field
in SuperType
.
All these mechanisms create conflicts to some degree because the properties share the same name yet might represent two distinct values.
Spring Data skips super-type properties if types are not assignable.
That is, the type of the overridden property must be assignable to its super-type property type to be registered as override, otherwise the super-type property is considered transient.
We generally recommend using distinct property names.
Spring Data modules generally support overridden properties holding different values. From a programming model perspective there are a few things to consider:
-
Which property should be persisted (default to all declared properties)? You can exclude properties by annotating these with
@Transient
. -
How to represent properties in your data store? Using the same field/column name for different values typically leads to corrupt data so you should annotate least one of the properties using an explicit field/column name.
-
Using
@AccessType(PROPERTY)
cannot be used as the super-property cannot be generally set without making any further assumptions of the setter implementation.
18.1.4. Kotlin support
Spring Data adapts specifics of Kotlin to allow object creation and mutation.
Kotlin object creation
Kotlin classes are supported to be instantiated , all classes are immutable by default and require explicit property declarations to define mutable properties.
Consider the following data
class Person
:
data class Person(val id: String, val name: String)
The class above compiles to a typical class with an explicit constructor.We can customize this class by adding another constructor and annotate it with @PersistenceConstructor
to indicate a constructor preference:
data class Person(var id: String, val name: String) {
@PersistenceConstructor
constructor(id: String) : this(id, "unknown")
}
Kotlin supports parameter optionality by allowing default values to be used if a parameter is not provided.
When Spring Data detects a constructor with parameter defaulting, then it leaves these parameters absent if the data store does not provide a value (or simply returns null
) so Kotlin can apply parameter defaulting.Consider the following class that applies parameter defaulting for name
data class Person(var id: String, val name: String = "unknown")
Every time the name
parameter is either not part of the result or its value is null
, then the name
defaults to unknown
.
Property population of Kotlin data classes
In Kotlin, all classes are immutable by default and require explicit property declarations to define mutable properties.
Consider the following data
class Person
:
data class Person(val id: String, val name: String)
This class is effectively immutable.
It allows creating new instances as Kotlin generates a copy(…)
method that creates new object instances copying all property values from the existing object and applying property values provided as arguments to the method.
Kotlin Overriding Properties
Kotlin allows declaring property overrides to alter properties in subclasses.
open class SuperType(open var field: Int)
class SubType(override var field: Int = 1) :
SuperType(field) {
}
Such an arrangement renders two properties with the name field
.
Kotlin generates property accessors (getters and setters) for each property in each class.
Effectively, the code looks like as follows:
public class SuperType {
private int field;
public SuperType(int field) {
this.field = field;
}
public int getField() {
return this.field;
}
public void setField(int field) {
this.field = field;
}
}
public final class SubType extends SuperType {
private int field;
public SubType(int field) {
super(field);
this.field = field;
}
public int getField() {
return this.field;
}
public void setField(int field) {
this.field = field;
}
}
Getters and setters on SubType
set only SubType.field
and not SuperType.field
.
In such an arrangement, using the constructor is the only default approach to set SuperType.field
.
Adding a method to SubType
to set SuperType.field
via this.SuperType.field = …
is possible but falls outside of supported conventions.
Property overrides create conflicts to some degree because the properties share the same name yet might represent two distinct values.
We generally recommend using distinct property names.
Spring Data modules generally support overridden properties holding different values. From a programming model perspective there are a few things to consider:
-
Which property should be persisted (default to all declared properties)? You can exclude properties by annotating these with
@Transient
. -
How to represent properties in your data store? Using the same field/column name for different values typically leads to corrupt data so you should annotate least one of the properties using an explicit field/column name.
-
Using
@AccessType(PROPERTY)
cannot be used as the super-property cannot be set.
18.2. Convention-based Mapping
MappingMongoConverter
has a few conventions for mapping objects to documents when no additional mapping metadata is provided. The conventions are:
-
The short Java class name is mapped to the collection name in the following manner. The class
com.bigbank.SavingsAccount
maps to thesavingsAccount
collection name. -
All nested objects are stored as nested objects in the document and not as DBRefs.
-
The converter uses any Spring Converters registered with it to override the default mapping of object properties to document fields and values.
-
The fields of an object are used to convert to and from fields in the document. Public
JavaBean
properties are not used. -
If you have a single non-zero-argument constructor whose constructor argument names match top-level field names of document, that constructor is used. Otherwise, the zero-argument constructor is used. If there is more than one non-zero-argument constructor, an exception will be thrown.
18.2.1. How the _id
field is handled in the mapping layer.
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 MappingMongoConverter
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 annotated with
@Id
(org.springframework.data.annotation.Id
) will be mapped to the_id
field. -
A field without an annotation but named
id
will be mapped to the_id
field. -
The default field name for identifiers is
_id
and can be customized via the@Field
annotation.
Field definition | Resulting Id-Fieldname in MongoDB |
---|---|
|
|
|
|
|
|
|
|
|
|
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 forid
in your application, the conversion to an ObjectId is detected to the MongoDB driver. If the specifiedid
value cannot be converted to an ObjectId, then the value will be stored as is in the document’s _id field. This also applies if the field is annotated with@Id
. -
If a field is annotated with
@MongoId
in the Java class it will be converted to and stored as using its actual type. No further conversion happens unless@MongoId
declares a desired field type. -
If a field is annotated with
@MongoId(FieldType.…)
in the Java class it will be attempted to convert the value to the declaredFieldType.
-
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.
18.3. Data Mapping and Type Conversion
This section explains how types are mapped to and from a MongoDB representation. Spring Data MongoDB supports all types that can be represented as BSON, MongoDB’s internal document format. In addition to these types, Spring Data MongoDB provides a set of built-in converters to map additional types. You can provide your own converters to adjust type conversion. See [mapping-explicit-converters] for further details.
The following provides samples of each available type conversion:
Type | Type conversion | Sample |
---|---|---|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
Array, |
native |
|
|
native |
|
|
native |
|
|
native |
|
|
native |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
native |
|
|
converter |
|
|
converter / native (Java8)[2] |
|
|
converter / native (Java8)[3] |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
|
converter |
|
18.4. Mapping Configuration
Unless explicitly configured, an instance of MappingMongoConverter
is created by default when you create a MongoTemplate
. You can create your own instance of the MappingMongoConverter
. Doing so lets you dictate where in the classpath your domain classes can be found, so that Spring Data MongoDB can extract metadata and construct indexes. Also, by creating your own instance, you can register Spring converters to map specific classes to and from the database.
You can configure the MappingMongoConverter
as well as com.mongodb.client.MongoClient
and MongoTemplate by using either Java-based or XML-based metadata. The following example uses Spring’s Java-based configuration:
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Override
public String getDatabaseName() {
return "database";
}
// the following are optional
@Override
public String getMappingBasePackage() { (1)
return "com.bigbank.domain";
}
@Override
void configureConverters(MongoConverterConfigurationAdapter adapter) { (2)
adapter.registerConverter(new org.springframework.data.mongodb.test.PersonReadConverter());
adapter.registerConverter(new org.springframework.data.mongodb.test.PersonWriteConverter());
}
@Bean
public LoggingEventListener<MongoMappingEvent> mappingEventsListener() {
return new LoggingEventListener<MongoMappingEvent>();
}
}
1 | The mapping base package defines the root path used to scan for entities used to pre initialize the MappingContext . By default the configuration classes package is used. |
2 | Configure additional custom converters for specific domain types that replace the default mapping procedure for those types with your custom implementation. |
AbstractMongoClientConfiguration
requires you to implement methods that define a com.mongodb.client.MongoClient
as well as provide a database name. AbstractMongoClientConfiguration
also has a method named getMappingBasePackage(…)
that you can override to tell the converter where to scan for classes annotated with the @Document
annotation.
You can add additional converters to the converter by overriding the customConversionsConfiguration
method.
MongoDB’s native JSR-310 support can be enabled through MongoConverterConfigurationAdapter.useNativeDriverJavaTimeCodecs()
.
Also shown in the preceding example is a LoggingEventListener
, which logs MongoMappingEvent
instances that are posted onto Spring’s ApplicationContextEvent
infrastructure.
AbstractMongoClientConfiguration creates a MongoTemplate instance and registers it with the container under the name mongoTemplate .
|
Spring’s MongoDB namespace lets you enable mapping functionality in XML, as the following example shows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo-client host="localhost" port="27017"/>
<mongo:db-factory dbname="database" mongo-ref="mongoClient"/>
<!-- 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 id="readConverter" class="org.springframework.data.mongodb.test.PersonReadConverter"/>
<!-- set the mapping converter to be used by the MongoTemplate -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>
<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.
18.5. Metadata-based Mapping
To take full advantage of the object mapping functionality inside the Spring Data MongoDB support, you should annotate your mapped objects with the @Document
annotation.
Although it is not necessary for the mapping framework to have this annotation (your POJOs are mapped correctly, even without any annotations), it lets the classpath scanner find and pre-process your domain objects to extract the necessary metadata.
If you do not use this annotation, your application takes a slight performance hit the first time you store a domain object, because the mapping framework needs to build up its internal metadata model so that it knows about the properties of your domain object and how to persist them.
The following example shows a domain object:
package com.mycompany.domain;
@Document
public class Person {
@Id
private ObjectId id;
@Indexed
private Integer ssn;
private String firstName;
@Indexed
private String lastName;
}
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 createIndex(…) on that property of your document, making searches faster.
Automatic index creation is only done for types annotated with @Document .
|
Auto index creation is disabled by default and needs to be enabled through the configuration (see Index Creation). |
18.5.1. Index Creation
Spring Data MongoDB can automatically create indexes for entity types annotated with @Document
.
Index creation must be explicitly enabled since version 3.0 to prevent undesired effects with collection lifecyle and performance impact.
Indexes are automatically created for the initial entity set on application startup and when accessing an entity type for the first time while the application runs.
We generally recommend explicit index creation for application-based control of indexes as Spring Data cannot automatically create indexes for collections that were recreated while the application was running.
IndexResolver
provides an abstraction for programmatic index definition creation if you want to make use of @Indexed
annotations such as @GeoSpatialIndexed
, @TextIndexed
, @CompoundIndex
and @WildcardIndexed
.
You can use index definitions with IndexOperations
to create indexes.
A good point in time for index creation is on application startup, specifically after the application context was refreshed, triggered by observing ContextRefreshedEvent
.
This event guarantees that the context is fully initialized.
Note that at this time other components, especially bean factories might have access to the MongoDB database.
|
class MyListener {
@EventListener(ContextRefreshedEvent.class)
public void initIndicesAfterStartup() {
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = mongoTemplate
.getConverter().getMappingContext();
IndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
}
}
class MyListener{
@EventListener(ContextRefreshedEvent.class)
public void initIndicesAfterStartup() {
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = mongoTemplate
.getConverter().getMappingContext();
// consider only entities that are annotated with @Document
mappingContext.getPersistentEntities()
.stream()
.filter(it -> it.isAnnotationPresent(Document.class))
.forEach(it -> {
IndexOperations indexOps = mongoTemplate.indexOps(it.getType());
resolver.resolveIndexFor(it.getType()).forEach(indexOps::ensureIndex);
});
}
}
Alternatively, if you want to ensure index and collection presence before any component is able to access your database from your application, declare a @Bean
method for MongoTemplate
and include the code from above before returning the MongoTemplate
object.
To turn automatic index creation ON please override
|
Automatic index creation is turned OFF by default as of version 3.0. |
18.5.2. Mapping Annotation Overview
The MappingMongoConverter can use metadata to drive the mapping of objects to documents. The following annotations are available:
-
@Id
: Applied at the field level to mark the field used for identity purpose. -
@MongoId
: Applied at the field level to mark the field used for identity purpose. Accepts an optionalFieldType
to customize id conversion. -
@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 data will be stored. -
@DBRef
: Applied at the field to indicate it is to be stored using a com.mongodb.DBRef. -
@DocumentReference
: Applied at the field to indicate it is to be stored as a pointer to another document. This can be a single value (the id by default), or aDocument
provided via a converter. -
@Indexed
: Applied at the field level to describe how to index the field. -
@CompoundIndex
(repeatable): Applied at the type level to declare Compound Indexes. -
@GeoSpatialIndexed
: Applied at the field level to describe how to geoindex the field. -
@TextIndexed
: Applied at the field level to mark the field to be included in the text index. -
@HashIndexed
: Applied at the field level for usage within a hashed index to partition data across a sharded cluster. -
@Language
: Applied at the field level to set the language override property for text index. -
@Transient
: By default, all fields are mapped to the document. This annotation excludes the field where it is applied from being stored in the database. Transient properties cannot be used within a persistence constructor as the converter cannot materialize a value for the constructor argument. -
@PersistenceConstructor
: Marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved Document. -
@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")
whereroot
refers to the root of the given document. -
@Field
: Applied at the field level it allows to describe the name and type of the field as it will be represented in the MongoDB BSON document thus allowing the name and type to be different than the fieldname of the class as well as the property type. -
@Version
: Applied at field level is used for optimistic locking and checked for modification on save operations. The initial value iszero
(one
for primitive types) which is bumped automatically on every update.
The mapping metadata infrastructure is defined in a separate spring-data-commons project that is technology agnostic. Specific subclasses are using in the MongoDB support to support annotation based metadata. Other strategies are also possible to put in place if there is demand.
Here is an example of a more complex mapping.
@Document
@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;
private Integer age;
@Transient
private Integer accountTotal;
@DBRef
private List<Account> accounts;
private T address;
public Person(Integer ssn) {
this.ssn = ssn;
}
@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;
}
public String getId() {
return id;
}
// no setter for Id. (getter is only exposed for some unit testing)
public Integer getSsn() {
return ssn;
}
// other getters/setters omitted
}
You may even consider your own, custom annotation.
|
18.5.3. Customized Object Construction
The mapping subsystem allows the customization of the object construction by annotating a constructor with the @PersistenceConstructor
annotation. The values to be used for the constructor parameters are resolved in the following way:
-
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 a
MappingException
will be thrown indicating that the given constructor parameter could not be bound.
class OrderItem {
private @Id String id;
private int quantity;
private double unitPrice;
OrderItem(String id, @Value("#root.qty ?: 0") int quantity, double unitPrice) {
this.id = id;
this.quantity = quantity;
this.unitPrice = unitPrice;
}
// getters/setters ommitted
}
Document input = new Document("id", "4711");
input.put("unitPrice", 2.5);
input.put("qty",5);
OrderItem item = converter.read(OrderItem.class, input);
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.
18.5.4. Compound Indexes
Compound indexes are also supported. They are defined at the class level, rather than on individual properties.
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
@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;
}
|
18.5.5. Hashed Indexes
Hashed indexes allow hash based sharding within a sharded cluster. Using hashed field values to shard collections results in a more random distribution. For details, refer to the MongoDB Documentation.
Here’s an example that creates a hashed index for _id
:
@Document
public class DomainType {
@HashIndexed @Id String id;
// ...
}
Hashed indexes can be created next to other index definitions like shown below, in that case both indices are created:
@Document
public class DomainType {
@Indexed
@HashIndexed
String value;
// ...
}
In case the example above is too verbose, a compound annotation allows to reduce the number of annotations that need to be declared on a property:
@Document
public class DomainType {
@IndexAndHash(name = "idx...") (1)
String value;
// ...
}
@Indexed
@HashIndexed
@Retention(RetentionPolicy.RUNTIME)
public @interface IndexAndHash {
@AliasFor(annotation = Indexed.class, attribute = "name") (1)
String name() default "";
}
1 | Potentially register an alias for certain attributes of the meta annotation. |
Although index creation via annotations comes in handy for many scenarios cosider taking over more control by setting up indices manually via
|
18.5.6. Wildcard Indexes
A WildcardIndex
is an index that can be used to include all fields or specific ones based a given (wildcard) pattern.
For details, refer to the MongoDB Documentation.
The index can be set up programmatically using WildcardIndex
via IndexOperations
.
mongoOperations
.indexOps(User.class)
.ensureIndex(new WildcardIndex("userMetadata"));
db.user.createIndex({ "userMetadata.$**" : 1 }, {})
The @WildcardIndex
annotation allows a declarative index setup that can used either with a document type or property.
If placed on a type that is a root level domain entity (one annotated with @Document
) , the index resolver will create a
wildcard index for it.
@Document
@WildcardIndexed
public class Product {
// …
}
db.product.createIndex({ "$**" : 1 },{})
The wildcardProjection
can be used to specify keys to in-/exclude in the index.
wildcardProjection
@Document
@WildcardIndexed(wildcardProjection = "{ 'userMetadata.age' : 0 }")
public class User {
private @Id String id;
private UserMetadata userMetadata;
}
db.user.createIndex(
{ "$**" : 1 },
{ "wildcardProjection" :
{ "userMetadata.age" : 0 }
}
)
Wildcard indexes can also be expressed by adding the annotation directly to the field.
Please note that wildcardProjection
is not allowed on nested paths such as properties.
Projections on types annotated with @WildcardIndexed
are omitted during index creation.
@Document
public class User {
private @Id String id;
@WildcardIndexed
private UserMetadata userMetadata;
}
db.user.createIndex({ "userMetadata.$**" : 1 }, {})
18.5.7. Text Indexes
The text index feature is disabled by default for MongoDB v.2.4. |
Creating a text index allows accumulating several fields into a searchable full-text index.
It is only possible to have one text index per collection, so all fields marked with @TextIndexed
are combined into this index.
Properties can be weighted to influence the document score for ranking results.
The default language for the text index is English.To change the default language, set the language
attribute to whichever language you want (for example,@Document(language="spanish")
).
Using a property called language
or @Language
lets you define a language override on a per-document base.
The following example shows how to created a text index and set the language to Spanish:
@Document(language = "spanish")
class SomeEntity {
@TextIndexed String foo;
@Language String lang;
Nested nested;
}
class Nested {
@TextIndexed(weight=5) String bar;
String roo;
}
18.5.8. Using DBRefs
The mapping framework does not 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 are eagerly resolved so that you get back a mapped object that looks the same as if it had been stored embedded within your top-level document.
The following example uses 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;
}
You need not use @OneToMany
or similar mechanisms because the List of objects tells the mapping framework that you want a one-to-many relationship.
When the object is stored in MongoDB, there is a list of DBRefs rather than the Account
objects themselves.
When it comes to loading collections of DBRef
s it is advisable to restrict references held in collection types to a specific MongoDB collection.
This allows bulk loading of all references, whereas references pointing to different MongoDB collections need to be resolved one by one.
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 does not automatically save the Account objects in the accounts property.
|
DBRef
s can also be resolved lazily.
In this case the actual Object
or Collection
of references is resolved on first access of the property.
Use the lazy
attribute of @DBRef
to specify this.
Required properties that are also defined as lazy loading DBRef
and used as constructor arguments are also decorated with the lazy loading proxy making sure to put as little pressure on the database and network as possible.
Lazily loaded DBRef s can be hard to debug.
Make sure tooling does not accidentally trigger proxy resolution by e.g. calling toString() or some inline debug rendering invoking property getters.
Please consider to enable trace logging for org.springframework.data.mongodb.core.convert.DefaultDbRefResolver to gain insight on DBRef resolution.
|
Lazy loading may require class proxies, that in turn, might need access to jdk internals, that are not open, starting with Java 16+, due to JEP 396: Strongly Encapsulate JDK Internals by Default.
For those cases please consider falling back to an interface type (eg. switch from ArrayList to List ) or provide the required --add-opens argument.
|
18.5.9. Using Document References
Using @DocumentReference
offers a flexible way of referencing entities in MongoDB.
While the goal is the same as when using DBRefs, the store representation is different.
DBRef
resolves to a document with a fixed structure as outlined in the MongoDB Reference documentation.
Document references, do not follow a specific format.
They can be literally anything, a single value, an entire document, basically everything that can be stored in MongoDB.
By default, the mapping layer will use the referenced entities id value for storage and retrieval, like in the sample below.
@Document
class Account {
@Id
String id;
Float total;
}
@Document
class Person {
@Id
String id;
@DocumentReference (1)
List<Account> accounts;
}
Account account = …
tempate.insert(account); (2)
template.update(Person.class)
.matching(where("id").is(…))
.apply(new Update().push("accounts").value(account)) (3)
.first();
{
"_id" : …,
"accounts" : [ "6509b9e" … ] (4)
}
1 | Mark the collection of Account values to be referenced. |
2 | The mapping framework does not handle cascading saves, so make sure to persist the referenced entity individually. |
3 | Add the reference to the existing entity. |
4 | Referenced Account entities are represented as an array of their _id values. |
The sample above uses an _id
-based fetch query ({ '_id' : ?#{#target} }
) for data retrieval and resolves linked entities eagerly.
It is possible to alter resolution defaults (listed below) using the attributes of @DocumentReference
Attribute | Description | Default |
---|---|---|
|
The target database name for collection lookup. |
|
|
The target collection name. |
The annotated property’s domain type, respectively the value type in case of |
|
The single document lookup query evaluating placeholders via SpEL expressions using |
An |
|
Used for sorting result documents on server side. |
None by default.
Result order of |
|
If set to |
Resolves properties eagerly by default. |
Lazy loading may require class proxies, that in turn, might need access to jdk internals, that are not open, starting with Java 16+, due to JEP 396: Strongly Encapsulate JDK Internals by Default.
For those cases please consider falling back to an interface type (eg. switch from ArrayList to List ) or provide the required --add-opens argument.
|
@DocumentReference(lookup)
allows defining filter queries that can be different from the _id
field and therefore offer a flexible way of defining references between entities as demonstrated in the sample below, where the Publisher
of a book is referenced by its acronym instead of the internal id
.
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
@Field("publisher_ac")
@DocumentReference(lookup = "{ 'acronym' : ?#{#target} }") (1)
Publisher publisher;
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym; (1)
String name;
@DocumentReference(lazy = true) (2)
List<Book> books;
}
Book
document{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisher_ac" : "DR"
}
Publisher
document{
"_id" : 1a23e45,
"acronym" : "DR",
"name" : "Del Rey",
…
}
1 | Use the acronym field to query for entities in the Publisher collection. |
2 | Lazy load back references to the Book collection. |
The above snippet shows the reading side of things when working with custom referenced objects.
Writing requires a bit of additional setup as the mapping information do not express where #target
stems from.
The mapping layer requires registration of a Converter
between the target document and DocumentPointer
, like the one below:
@WritingConverter
class PublisherReferenceConverter implements Converter<Publisher, DocumentPointer<String>> {
@Override
public DocumentPointer<String> convert(Publisher source) {
return () -> source.getAcronym();
}
}
If no DocumentPointer
converter is provided the target reference document can be computed based on the given lookup query.
In this case the association target properties are evaluated as shown in the following sample.
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
@DocumentReference(lookup = "{ 'acronym' : ?#{acc} }") (1) (2)
Publisher publisher;
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym; (1)
String name;
// ...
}
{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisher" : {
"acc" : "DOC"
}
}
1 | Use the acronym field to query for entities in the Publisher collection. |
2 | The field value placeholders of the lookup query (like acc ) is used to form the reference document. |
It is also possible to model relational style One-To-Many references using a combination of @ReadonlyProperty
and @DocumentReference
.
This approach allows link types without storing the linking values within the owning document but rather on the referencing document as shown in the example below.
@Document
class Book {
@Id
ObjectId id;
String title;
List<String> author;
ObjectId publisherId; (1)
}
@Document
class Publisher {
@Id
ObjectId id;
String acronym;
String name;
@ReadOnlyProperty (2)
@DocumentReference(lookup="{'publisherId':?#{#self._id} }") (3)
List<Book> books;
}
Book
document{
"_id" : 9a48e32,
"title" : "The Warded Man",
"author" : ["Peter V. Brett"],
"publisherId" : 8cfb002
}
Publisher
document{
"_id" : 8cfb002,
"acronym" : "DR",
"name" : "Del Rey"
}
1 | Set up the link from Book (reference) to Publisher (owner) by storing the Publisher.id within the Book document. |
2 | Mark the property holding the references to be readonly.
This prevents storing references to individual Book s with the Publisher document. |
3 | Use the #self variable to access values within the Publisher document and in this retrieve Books with matching publisherId . |
With all the above in place it is possible to model all kind of associations between entities. Have a look at the non-exhaustive list of samples below to get feeling for what is possible.
class Entity {
@DocumentReference
ReferencedObject ref;
}
// entity
{
"_id" : "8cfb002",
"ref" : "9a48e32" (1)
}
// referenced object
{
"_id" : "9a48e32" (1)
}
1 | MongoDB simple type can be directly used without further configuration. |
class Entity {
@DocumentReference(lookup = "{ '_id' : '?#{#target}' }") (1)
ReferencedObject ref;
}
// entity
{
"_id" : "8cfb002",
"ref" : "9a48e32" (1)
}
// referenced object
{
"_id" : "9a48e32"
}
1 | target defines the reference value itself. |
refKey
field for the lookup queryclass Entity {
@DocumentReference(lookup = "{ '_id' : '?#{refKey}' }") (1) (2)
private ReferencedObject ref;
}
@WritingConverter
class ToDocumentPointerConverter implements Converter<ReferencedObject, DocumentPointer<Document>> {
public DocumentPointer<Document> convert(ReferencedObject source) {
return () -> new Document("refKey", source.id); (1)
}
}
// entity
{
"_id" : "8cfb002",
"ref" : {
"refKey" : "9a48e32" (1)
}
}
// referenced object
{
"_id" : "9a48e32"
}
1 | The key used for obtaining the reference value must be the one used during write. |
2 | refKey is short for target.refKey . |
class Entity {
@DocumentReference(lookup = "{ 'firstname' : '?#{fn}', 'lastname' : '?#{ln}' }") (1) (2)
ReferencedObject ref;
}
// entity
{
"_id" : "8cfb002",
"ref" : {
"fn" : "Josh", (1)
"ln" : "Long" (1)
}
}
// referenced object
{
"_id" : "9a48e32",
"firsntame" : "Josh", (2)
"lastname" : "Long", (2)
}
1 | Read/wirte the keys fn & ln from/to the linkage document based on the lookup query. |
2 | Use non id fields for the lookup of the target documents. |
class Entity {
@DocumentReference(lookup = "{ '_id' : '?#{id}' }", collection = "?#{collection}") (2)
private ReferencedObject ref;
}
@WritingConverter
class ToDocumentPointerConverter implements Converter<ReferencedObject, DocumentPointer<Document>> {
public DocumentPointer<Document> convert(ReferencedObject source) {
return () -> new Document("id", source.id) (1)
.append("collection", … ); (2)
}
}
// entity
{
"_id" : "8cfb002",
"ref" : {
"id" : "9a48e32", (1)
"collection" : "…" (2)
}
}
1 | Read/wirte the keys _id from/to the reference document to use them in the lookup query. |
2 | The collection name can be read from the reference document using its key. |
We know it is tempting to use all kinds of MongoDB query operators in the lookup query and this is fine. But there a few aspects to consider:
A few more general remarks:
|
18.5.10. Mapping Framework Events
Events are fired throughout the lifecycle of the mapping process. This is described in the Lifecycle Events section.
Declaring these beans in your Spring ApplicationContext causes them to be invoked whenever the event is dispatched.
18.6. Unwrapping Types
Unwrapped entities are used to design value objects in your Java domain model whose properties are flattened out into the parent’s MongoDB Document.
18.6.1. Unwrapped Types Mapping
Consider the following domain model where User.name
is annotated with @Unwrapped
.
The @Unwrapped
annotation signals that all properties of UserName
should be flattened out into the user
document that owns the name
property.
class User {
@Id
String userId;
@Unwrapped(onEmpty = USE_NULL) (1)
UserName name;
}
class UserName {
String firstname;
String lastname;
}
{
"_id" : "1da2ba06-3ba7",
"firstname" : "Emma",
"lastname" : "Frost"
}
1 | When loading the name property its value is set to null if both firstname and lastname are either null or not present.
By using onEmpty=USE_EMPTY an empty UserName , with potential null value for its properties, will be created. |
For less verbose embeddable type declarations use @Unwrapped.Nullable
and @Unwrapped.Empty
instead @Unwrapped(onEmpty = USE_NULL)
and @Unwrapped(onEmpty = USE_EMPTY)
.
Both annotations are meta-annotated with JSR-305 @javax.annotation.Nonnull
to aid with nullability inspections.
It is possible to use complex types within an unwrapped object. However, those must not be, nor contain unwrapped fields themselves. |
18.6.2. Unwrapped Types field names
A value object can be unwrapped multiple times by using the optional prefix
attribute of the @Unwrapped
annotation.
By dosing so the chosen prefix is prepended to each property or @Field("…")
name in the unwrapped object.
Please note that values will overwrite each other if multiple properties render to the same field name.
class User {
@Id
String userId;
@Unwrapped.Nullable(prefix = "u_") (1)
UserName name;
@Unwrapped.Nullable(prefix = "a_") (2)
UserName name;
}
class UserName {
String firstname;
String lastname;
}
{
"_id" : "a6a805bd-f95f",
"u_firstname" : "Jean", (1)
"u_lastname" : "Grey",
"a_firstname" : "Something", (2)
"a_lastname" : "Else"
}
1 | All properties of UserName are prefixed with u_ . |
2 | All properties of UserName are prefixed with a_ . |
While combining the @Field
annotation with @Unwrapped
on the very same property does not make sense and therefore leads to an error.
It is a totally valid approach to use @Field
on any of the unwrapped types properties.
@Field
annotationpublic class User {
@Id
private String userId;
@Unwrapped.Nullable(prefix = "u-") (1)
UserName name;
}
public class UserName {
@Field("first-name") (2)
private String firstname;
@Field("last-name")
private String lastname;
}
{
"_id" : "2647f7b9-89da",
"u-first-name" : "Barbara", (2)
"u-last-name" : "Gordon"
}
1 | All properties of UserName are prefixed with u- . |
2 | Final field names are a result of concatenating @Unwrapped(prefix) and @Field(name) . |
18.6.3. Query on Unwrapped Objects
Defining queries on unwrapped properties is possible on type- as well as field-level as the provided Criteria
is matched against the domain type.
Prefixes and potential custom field names will be considered when rendering the actual query.
Use the property name of the unwrapped object to match against all contained fields as shown in the sample below.
UserName userName = new UserName("Carol", "Danvers")
Query findByUserName = query(where("name").is(userName));
User user = template.findOne(findByUserName, User.class);
db.collection.find({
"firstname" : "Carol",
"lastname" : "Danvers"
})
It is also possible to address any field of the unwrapped object directly using its property name as shown in the snippet below.
Query findByUserFirstName = query(where("name.firstname").is("Shuri"));
List<User> users = template.findAll(findByUserFirstName, User.class);
db.collection.find({
"firstname" : "Shuri"
})
Sort by unwrapped field.
Fields of unwrapped objects can be used for sorting via their property path as shown in the sample below.
Query findByUserLastName = query(where("name.lastname").is("Romanoff"));
List<User> user = template.findAll(findByUserName.withSort(Sort.by("name.firstname")), User.class);
db.collection.find({
"lastname" : "Romanoff"
}).sort({ "firstname" : 1 })
Though possible, using the unwrapped object itself as sort criteria includes all of its fields in unpredictable order and may result in inaccurate ordering. |
Field projection on unwrapped objects
Fields of unwrapped objects can be subject for projection either as a whole or via single fields as shown in the samples below.
Query findByUserLastName = query(where("name.firstname").is("Gamora"));
findByUserLastName.fields().include("name"); (1)
List<User> user = template.findAll(findByUserName, User.class);
db.collection.find({
"lastname" : "Gamora"
},
{
"firstname" : 1,
"lastname" : 1
})
1 | A field projection on an unwrapped object includes all of its properties. |
Query findByUserLastName = query(where("name.lastname").is("Smoak"));
findByUserLastName.fields().include("name.firstname"); (1)
List<User> user = template.findAll(findByUserName, User.class);
db.collection.find({
"lastname" : "Smoak"
},
{
"firstname" : 1
})
1 | A field projection on an unwrapped object includes all of its properties. |
Query By Example on unwrapped object.
Unwrapped objects can be used within an Example
probe just as any other type.
Please review the Query By Example section, to learn more about this feature.
Repository Queries on unwrapped objects.
The Repository
abstraction allows deriving queries on fields of unwrapped objects as well as the entire object.
interface UserRepository extends CrudRepository<User, String> {
List<User> findByName(UserName username); (1)
List<User> findByNameFirstname(String firstname); (2)
}
1 | Matches against all fields of the unwrapped object. |
2 | Matches against the firstname . |
Index creation for unwrapped objects is suspended even if the repository |
18.6.4. Update on Unwrapped Objects
Unwrapped objects can be updated as any other object that is part of the domain model. The mapping layer takes care of flattening structures into their surroundings. It is possible to update single attributes of the unwrapped object as well as the entire value as shown in the examples below.
Update update = new Update().set("name.firstname", "Janet");
template.update(User.class).matching(where("id").is("Wasp"))
.apply(update).first()
db.collection.update({
"_id" : "Wasp"
},
{
"$set" { "firstname" : "Janet" }
},
{ ... }
)
Update update = new Update().set("name", new Name("Janet", "van Dyne"));
template.update(User.class).matching(where("id").is("Wasp"))
.apply(update).first()
db.collection.update({
"_id" : "Wasp"
},
{
"$set" {
"firstname" : "Janet",
"lastname" : "van Dyne",
}
},
{ ... }
)
18.6.5. Aggregations on Unwrapped Objects
The Aggregation Framework will attempt to map unwrapped values of typed aggregations. Please make sure to work with the property path including the wrapper object when referencing one of its values. Other than that no special action is required.
18.6.6. Index on Unwrapped Objects
It is possible to attach the @Indexed
annotation to properties of an unwrapped type just as it is done with regular objects.
It is not possible to use @Indexed
along with the @Unwrapped
annotation on the owning property.
public class User {
@Id
private String userId;
@Unwrapped(onEmpty = USE_NULL)
UserName name; (1)
// Invalid -> InvalidDataAccessApiUsageException
@Indexed (2)
@Unwrapped(onEmpty = USE_Empty)
Address address;
}
public class UserName {
private String firstname;
@Indexed
private String lastname; (1)
}
1 | Index created for lastname in users collection. |
2 | Invalid @Indexed usage along with @Unwrapped |
18.7. Custom Conversions - Overriding Default Mapping
The most trivial way of influencing the mapping result is by specifying the desired native MongoDB target type via the
@Field
annotation. This allows to work with non MongoDB types like BigDecimal
in the domain model while persisting
values in native org.bson.types.Decimal128
format.
public class Payment {
@Id String id; (1)
@Field(targetType = FieldType.DECIMAL128) (2)
BigDecimal value;
Date date; (3)
}
{
"_id" : ObjectId("5ca4a34fa264a01503b36af8"), (1)
"value" : NumberDecimal(2.099), (2)
"date" : ISODate("2019-04-03T12:11:01.870Z") (3)
}
1 | String id values that represent a valid ObjectId are converted automatically. See How the _id Field is Handled in the Mapping Layer
for details. |
2 | The desired target type is explicitly defined as Decimal128 which translates to NumberDecimal . Otherwise the
BigDecimal value would have been truned into a String . |
3 | Date values are handled by the MongoDB driver itself an are stored as ISODate . |
The snippet above is handy for providing simple type hints. To gain more fine-grained control over the mapping process,
you can register Spring converters with the MongoConverter
implementations, such as the MappingMongoConverter
.
The MappingMongoConverter
checks to see if any Spring converters 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
.
For more information on the Spring type conversion service, see the reference docs here. |
18.7.1. Saving by Using a Registered Spring Converter
The following example shows an implementation of the Converter
that converts from a Person
object to a org.bson.Document
:
import org.springframework.core.convert.converter.Converter;
import org.bson.Document;
public class PersonWriteConverter implements Converter<Person, Document> {
public Document convert(Person source) {
Document document = new Document();
document.put("_id", source.getId());
document.put("name", source.getFirstName());
document.put("age", source.getAge());
return document;
}
}
18.7.2. Reading by Using a Spring Converter
The following example shows an implementation of a Converter
that converts from a Document
to a Person
object:
public class PersonReadConverter implements Converter<Document, Person> {
public Person convert(Document source) {
Person p = new Person((ObjectId) source.get("_id"), (String) source.get("name"));
p.setAge((Integer) source.get("age"));
return p;
}
}
18.7.3. Registering Spring Converters with the MongoConverter
class MyMongoConfiguration extends AbstractMongoClientConfiguration {
@Override
public String getDatabaseName() {
return "database";
}
@Override
protected void configureConverters(MongoConverterConfigurationAdapter adapter) {
adapter.registerConverter(new com.example.PersonReadConverter());
adapter.registerConverter(new com.example.PersonWriteConverter());
}
}
The following example of a Spring Converter
implementation converts from a String
to a custom Email
value object:
@ReadingConverter
public class EmailReadConverter implements Converter<String, Email> {
public Email convert(String source) {
return Email.valueOf(source);
}
}
If you write a Converter
whose source and target type are native types, we cannot determine whether we should consider it as a reading or a writing converter.
Registering the converter instance as both might lead to unwanted results.
For example, a Converter<String, Long>
is ambiguous, although it probably does not make sense to try to convert all String
instances into Long
instances when writing.
To let you force the infrastructure to register a converter for only one way, we provide @ReadingConverter
and @WritingConverter
annotations to be used in the converter implementation.
Converters are subject to explicit registration as instances are not picked up from a classpath or container scan to avoid unwanted registration with a conversion service and the side effects resulting from such a registration. Converters are registered with CustomConversions
as the central facility that allows registration and querying for registered converters based on source- and target type.
CustomConversions
ships with a pre-defined set of converter registrations:
-
JSR-310 Converters for conversion between
java.time
,java.util.Date
andString
types. -
Deprecated: Joda Time Converters for conversion between
org.joda.time
, JSR-310, andjava.util.Date
. -
Deprecated: ThreeTenBackport Converters for conversion between
org.joda.time
, JSR-310, andjava.util.Date
.
Default converters for local temporal types (e.g. LocalDateTime to java.util.Date ) rely on system-default timezone settings to convert between those types. You can override the default converter, by registering your own 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 the underlying data access API can handle natively, we register the converter instance as a reading or a writing converter.
The following examples show a writing- and a read converter (note the difference is in the order of the qualifiers on Converter
):
// Write converter as only the target type is one that can be handled natively
class MyConverter implements Converter<Person, String> { … }
// Read converter as only the source type is one that can be handled natively
class MyConverter implements Converter<String, Person> { … }
19. Sharding
MongoDB supports large data sets via sharding, a method for distributing data across multiple database servers. Please refer to the MongoDB Documentation to learn how to set up a sharded cluster, its requirements and limitations.
Spring Data MongoDB uses the @Sharded
annotation to identify entities stored in sharded collections as shown below.
@Document("users")
@Sharded(shardKey = { "country", "userId" }) (1)
public class User {
@Id
Long id;
@Field("userid")
String userId;
String country;
}
1 | The properties of the shard key get mapped to the actual field names. |
19.1. Sharded Collections
Spring Data MongoDB does not auto set up sharding for collections nor indexes required for it. The snippet below shows how to do so using the MongoDB client API.
MongoDatabase adminDB = template.getMongoDbFactory()
.getMongoDatabase("admin"); (1)
adminDB.runCommand(new Document("enableSharding", "db")); (2)
Document shardCmd = new Document("shardCollection", "db.users") (3)
.append("key", new Document("country", 1).append("userid", 1)); (4)
adminDB.runCommand(shardCmd);
1 | Sharding commands need to be run against the admin database. |
2 | Enable sharding for a specific database if necessary. |
3 | Shard a collection within the database having sharding enabled. |
4 | Specify the shard key. This example uses range based sharding. |
19.2. Shard Key Handling
The shard key consists of a single or multiple properties that must exist in every document in the target collection. It is used to distribute documents across shards.
Adding the @Sharded
annotation to an entity enables Spring Data MongoDB to apply best effort optimisations required for sharded scenarios.
This means essentially adding required shard key information, if not already present, to replaceOne
filter queries when upserting entities.
This may require an additional server round trip to determine the actual value of the current shard key.
By setting @Sharded(immutableKey = true) Spring Data does not attempt to check if an entity shard key was changed.
|
Please see the MongoDB Documentation for further details. The following list contains which operations are eligible for shard key auto-inclusion:
-
(Reactive)CrudRepository.save(…)
-
(Reactive)CrudRepository.saveAll(…)
-
(Reactive)MongoTemplate.save(…)
20. Kotlin Support
Kotlin is a statically typed language that targets the JVM (and other platforms) which allows writing concise and elegant code while providing excellent interoperability with existing libraries written in Java.
Spring Data provides first-class support for Kotlin and lets developers write Kotlin applications almost as if Spring Data was a Kotlin native framework.
The easiest way to build a Spring application with Kotlin is to leverage Spring Boot and its dedicated Kotlin support. This comprehensive tutorial will teach you how to build Spring Boot applications with Kotlin using start.spring.io.
20.1. Requirements
Spring Data supports Kotlin 1.3 and requires kotlin-stdlib
(or one of its variants, such as kotlin-stdlib-jdk8
) and kotlin-reflect
to be present on the classpath.
Those are provided by default if you bootstrap a Kotlin project via start.spring.io.
20.2. Null Safety
One of Kotlin’s key features is null safety, which cleanly deals with null
values at compile time.
This makes applications safer through nullability declarations and the expression of “value or no value” semantics without paying the cost of wrappers, such as Optional
.
(Kotlin allows using functional constructs with nullable values. See this comprehensive guide to Kotlin null safety.)
Although Java does not let you express null safety in its type system, Spring Data API is annotated with JSR-305 tooling friendly annotations declared in the org.springframework.lang
package.
By default, types from Java APIs used in Kotlin are recognized as platform types, for which null checks are relaxed.
Kotlin support for JSR-305 annotations and Spring nullability annotations provide null safety for the whole Spring Data API to Kotlin developers, with the advantage of dealing with null
related issues at compile time.
See Null Handling of Repository Methods how null safety applies to Spring Data Repositories.
You can configure JSR-305 checks by adding the For Kotlin versions 1.1+, the default behavior is the same as |
Generic type arguments, varargs, and array elements nullability are not supported yet, but should be in an upcoming release. |
20.3. Object Mapping
See Kotlin support for details on how Kotlin objects are materialized.
20.4. Extensions
Kotlin extensions provide the ability to extend existing classes with additional functionality. Spring Data Kotlin APIs use these extensions to add new Kotlin-specific conveniences to existing Spring APIs.
Keep in mind that Kotlin extensions need to be imported to be used. Similar to static imports, an IDE should automatically suggest the import in most cases. |
For example, Kotlin reified type parameters provide a workaround for JVM generics type erasure, and Spring Data provides some extensions to take advantage of this feature. This allows for a better Kotlin API.
To retrieve a list of SWCharacter
objects in Java, you would normally write the following:
Flux<SWCharacter> characters = template.find(SWCharacter.class).inCollection("star-wars").all()
With Kotlin and the Spring Data extensions, you can instead write the following:
val characters = template.find<SWCharacter>().inCollection("star-wars").all()
// or (both are equivalent)
val characters : Flux<SWCharacter> = template.find().inCollection("star-wars").all()
As in Java, characters
in Kotlin is strongly typed, but Kotlin’s clever type inference allows for shorter syntax.
Spring Data MongoDB provides the following extensions:
-
Reified generics support for
MongoOperations
,ReactiveMongoOperations
,FluentMongoOperations
,ReactiveFluentMongoOperations
, andCriteria
. -
Coroutines extensions for
ReactiveFluentMongoOperations
.
20.5. Coroutines
Kotlin Coroutines are lightweight threads allowing to write non-blocking code imperatively.
On language side, suspend
functions provides an abstraction for asynchronous operations while on library side kotlinx.coroutines provides functions like async { }
and types like Flow
.
Spring Data modules provide support for Coroutines on the following scope:
20.5.1. Dependencies
Coroutines support is enabled when kotlinx-coroutines-core
, kotlinx-coroutines-reactive
and kotlinx-coroutines-reactor
dependencies are in the classpath:
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactor</artifactId>
</dependency>
Supported versions 1.3.0 and above.
|
20.5.2. How Reactive translates to Coroutines?
For return values, the translation from Reactive to Coroutines APIs is the following:
-
fun handler(): Mono<Void>
becomessuspend fun handler()
-
fun handler(): Mono<T>
becomessuspend fun handler(): T
orsuspend fun handler(): T?
depending on if theMono
can be empty or not (with the advantage of being more statically typed) -
fun handler(): Flux<T>
becomesfun handler(): Flow<T>
Flow
is Flux
equivalent in Coroutines world, suitable for hot or cold stream, finite or infinite streams, with the following main differences:
-
Flow
is push-based whileFlux
is push-pull hybrid -
Backpressure is implemented via suspending functions
-
Flow
has only a single suspendingcollect
method and operators are implemented as extensions -
Operators are easy to implement thanks to Coroutines
-
Extensions allow adding custom operators to
Flow
-
Collect operations are suspending functions
-
map
operator supports asynchronous operation (no need forflatMap
) since it takes a suspending function parameter
Read this blog post about Going Reactive with Spring, Coroutines and Kotlin Flow for more details, including how to run code concurrently with Coroutines.
20.5.3. Repositories
Here is an example of a Coroutines repository:
interface CoroutineRepository : CoroutineCrudRepository<User, String> {
suspend fun findOne(id: String): User
fun findByFirstname(firstname: String): Flow<User>
suspend fun findAllByFirstname(id: String): List<User>
}
Coroutines repositories are built on reactive repositories to expose the non-blocking nature of data access through Kotlin’s Coroutines.
Methods on a Coroutines repository can be backed either by a query method or a custom implementation.
Invoking a custom implementation method propagates the Coroutines invocation to the actual implementation method if the custom method is suspend
-able without requiring the implementation method to return a reactive type such as Mono
or Flux
.
Note that depending on the method declaration the coroutine context may or may not be available.
To retain access to the context, either declare your method using suspend
or return a type that enables context propagation such as Flow
.
-
suspend fun findOne(id: String): User
: Retrieve the data once and synchronously by suspending. -
fun findByFirstname(firstname: String): Flow<User>
: Retrieve a stream of data. TheFlow
is created eagerly while data is fetched uponFlow
interaction (Flow.collect(…)
). -
fun getUser(): User
: Retrieve data once blocking the thread and without context propagation. This should be avoided.
Coroutines repositories are only discovered when the repository extends the CoroutineCrudRepository interface.
|
21. JMX support
The JMX support for MongoDB exposes the results of running the 'serverStatus' command on the admin database for a single MongoDB server instance. It also exposes an administrative MBean, MongoAdmin
, that lets you perform administrative operations, such as dropping or creating a database. The JMX features build upon the JMX feature set available in the Spring Framework. See here for more details.
21.1. MongoDB JMX Configuration
Spring’s Mongo namespace lets you enable JMX functionality, as the following example shows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
https://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Default bean name is 'mongo' -->
<mongo:mongo-client host="localhost" port="27017"/>
<!-- by default look for a Mongo object named 'mongo' -->
<mongo:jmx/>
<context:mbean-export/>
<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
<context:annotation-config/>
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean" p:port="1099" />
<!-- Expose JMX over RMI -->
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"
depends-on="registry"
p:objectName="connector:name=rmi"
p:serviceUrl="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector" />
</beans>
The preceding code exposes several MBeans:
-
AssertMetrics
-
BackgroundFlushingMetrics
-
BtreeIndexCounters
-
ConnectionMetrics
-
GlobalLockMetrics
-
MemoryMetrics
-
OperationCounters
-
ServerInfo
-
MongoAdmin
The following screenshot from JConsole shows the resulting configuration:
Appendix
Appendix A: Namespace reference
The <repositories />
Element
The <repositories />
element triggers the setup of the Spring Data repository infrastructure. The most important attribute is base-package
, which defines the package to scan for Spring Data repository interfaces. See “XML Configuration”. The following table describes the attributes of the <repositories />
element:
Name | Description |
---|---|
|
Defines the package to be scanned for repository interfaces that extend |
|
Defines the postfix to autodetect custom repository implementations. Classes whose names end with the configured postfix are considered as candidates. Defaults to |
|
Determines the strategy to be used to create finder queries. See “Query Lookup Strategies” for details. Defaults to |
|
Defines the location to search for a Properties file containing externally defined queries. |
|
Whether nested repository interface definitions should be considered. Defaults to |
Appendix B: Populators namespace reference
The <populator /> element
The <populator />
element allows to populate the a data store via the Spring Data repository infrastructure.[4]
Name | Description |
---|---|
|
Where to find the files to read the objects from the repository shall be populated with. |
Appendix C: Repository query keywords
Supported query method subject keywords
The following table lists the subject keywords generally supported by the Spring Data repository query derivation mechanism to express the predicate. Consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
Keyword | Description |
---|---|
|
General query method returning typically the repository type, a |
|
Exists projection, returning typically a |
|
Count projection returning a numeric result. |
|
Delete query method returning either no result ( |
|
Limit the query results to the first |
|
Use a distinct query to return only unique results. Consult the store-specific documentation whether that feature is supported. This keyword can occur in any place of the subject between |
Supported query method predicate keywords and modifiers
The following table lists the predicate keywords generally supported by the Spring Data repository query derivation mechanism. However, consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.
Logical keyword | Keyword expressions |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In addition to filter predicates, the following list of modifiers is supported:
Keyword | Description |
---|---|
|
Used with a predicate keyword for case-insensitive comparison. |
|
Ignore case for all suitable properties. Used somewhere in the query method predicate. |
|
Specify a static sorting order followed by the property path and direction (e. g. |
Appendix D: Repository query return types
Supported Query Return Types
The following table lists the return types generally supported by Spring Data repositories. However, consult the store-specific documentation for the exact list of supported return types, because some types listed here might not be supported in a particular store.
Geospatial types (such as GeoResult , GeoResults , and GeoPage ) are available only for data stores that support geospatial queries.
Some store modules may define their own result wrapper types.
|
Return type | Description |
---|---|
|
Denotes no return value. |
Primitives |
Java primitives. |
Wrapper types |
Java wrapper types. |
|
A unique entity. Expects the query method to return one result at most. If no result is found, |
|
An |
|
A |
|
A |
|
A Java 8 or Guava |
|
Either a Scala or Vavr |
|
A Java 8 |
|
A convenience extension of |
Types that implement |
Types that expose a constructor or |
Vavr |
Vavr collection types. See Support for Vavr Collections for details. |
|
A |
|
A Java 8 |
|
A |
|
A sized chunk of data with an indication of whether there is more data available. Requires a |
|
A |
|
A result entry with additional information, such as the distance to a reference location. |
|
A list of |
|
A |
|
A Project Reactor |
|
A Project Reactor |
|
A RxJava |
|
A RxJava |
|
A RxJava |