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.
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>{version}</version> </dependency> </dependencies>
-
Change the version of Spring in the pom.xml to be
<spring.framework.version>{springVersion}</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
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.
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.
Note
|
For those not familiar with how to configure the Spring container using Java-based bean metadata instead of XML-based metadata, see the high-level introduction in the reference docs here as well as the detailed documentation here. |
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 Mongo mongo;
field.
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>
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.
Note
|
Use SimpleMongoClientDbFactory when choosing com.mongodb.client.MongoClient as the entrypoint of choice.
|
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.
Note
|
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.
|
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>
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.
Note
|
Once configured, MongoTemplate is thread-safe and can be reused across multiple instances.
|
The mapping between MongoDB documents and domain classes is done by delegating to an implementation of the MongoConverter
interface. Spring provides MappingMongoConverter
, but you can also write your own converter. See “[mongo.custom-converters]” 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.
Note
|
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]” 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.
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.
Note
|
The preferred way to reference the operations on MongoTemplate instance is through its interface, MongoOperations .
|
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
.
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.
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();
}
}
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:
Note
|
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.
Note
|
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”.
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)
}
-
The id is treated as
String
without further conversion. -
The id is treated as
ObjectId
. -
The id is treated as
ObjectId
if the givenString
is a validObjectId
hex, otherwise asString
. Corresponds to@Id
usage.
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
.
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.
Warning
|
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. @Configuration
public class AppConfig extends AbstractMongoClientConfiguration {
@Override
protected Set<Class<?>> getInitialEntitySet() {
return Collections.singleton(Person.class);
}
// ...
} |
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"/>
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>
.
Note
|
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.
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.
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
.
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.
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.
-
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.
Warning
|
updateFirst does not support ordering. Please use findAndModify to apply Sort .
|
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");
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();
Warning
|
upsert does not support ordering. Please use findAndModify to apply Sort .
|
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();
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)
)
-
The 1st
$set
stage calculates a new field average based on the average of the tests field. -
The 2nd
$set
stage calculates a new field grade based on the average field calculated by the first aggregation stage. -
The pipeline is run on the students collection and uses
Student
for the aggregation field mapping. -
Apply the update to all matching documents in the collection.
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)
-
Use the fluent update API with the domain type given for mapping the query and deriving the collection name or just use
MongoOperations#findAndReplace
. -
The actual match query mapped against the given domain type. Provide
sort
,fields
andcollation
settings via the query. -
Additional optional hook to provide options other than the defaults, like
upsert
. -
An optional projection type used for mapping the operation result. If none given the initial domain type is used.
-
Trigger the actual processing. Use
findAndReplaceValue
to obtain the nullable result instead of anOptional
.
Important
|
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.
|
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)
-
Remove a single entity specified by its
_id
from the associated collection. -
Remove all documents that match the criteria of the query from the
GOT
collection. -
Remove the first three documents in the
GOT
collection. Unlike <2>, the documents to remove are identified by their_id
, running the given query, applyingsort
,limit
, andskip
options first, and then removing all at once in a separate step. -
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. -
Remove the first three documents in the
GOT
collection. Unlike <3>, documents do not get deleted in a batch but one by one.
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)
-
Intially insert document.
version
is set to0
. -
Load the just inserted document.
version
is still0
. -
Update the document with
version = 0
. Set thelastname
and bumpversion
to1
. -
Try to update the previously loaded document that still has
version = 0
. The operation fails with anOptimisticLockingFailureException
, as the currentversion
is1
.
Important
|
Optimistic Locking requires to set the WriteConcern to ACKNOWLEDGED . Otherwise OptimisticLockingFailureException can be silently swallowed.
|
Note
|
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) .
|
Note
|
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.
|
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.).
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.
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
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.
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
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)
-
Result will contain both
_id
andlast_name
via{ "last_name" : 1 }
. -
Result will only contain the
last_name
via{ "_id" : 0, "last_name" : 1 }
. -
Result will contain the
_id
and entireaddress
object via{ "address" : 1 }
. -
Result will contain the
_id
and andaddress
object that only contains thecity
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");
-
Use a native expression. The used field name must refer to field names within the database document.
-
Assign the field name to which the expression result is projected. The resulting field name is not mapped against the domain model.
-
Use an
AggregationExpression
. Other than nativeMongoExpression
, field names are mapped to the ones used in the domain model. -
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.repositories.queries.json-based].
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.
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)
-
Query the
Person
collection. -
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. -
Retrieve all distinct values as a
List
ofObject
(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)
-
Query the collection of
Person
. -
Select distinct values of the
lastname
field. The fieldname is mapped according to the domain types property declaration, taking potential@Field
annotations into account. -
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. -
Retrieve all distinct values as a
List
ofString
. If the type cannot be converted into the desired target type, this method throws aDataAccessException
.
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.
Note
|
Using GeoSpatial queries requires attention when used within MongoDB transactions, see [mongo.transactions.behavior]. |
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);
Warning
|
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. GeoResults<VenueWithDisField> = template.query(Venue.class) (1)
.as(VenueWithDisField.class) (2)
.near(NearQuery.near(new GeoJsonPoint(-73.99, 40.73), KILOMETERS))
.all();
|
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.
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.
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;
}
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)));
-
Repository method definition using the commons type allows calling it with both the GeoJSON and the legacy format.
-
Use GeoJSON type to make use of
$geometry
operator. -
Note that GeoJSON polygons need to define a closed ring.
-
Use the legacy format
$polygon
operator.
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.
Warning
|
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)
}
-
Maximum distance from center point in Meters.
-
GeoJSON always operates upon a sphere.
-
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)
}
-
Maximum distance from center point in Radians.
-
The distance multiplier so we get Kilometers as resulting distance.
-
Make sure we operate on a 2d_sphere index.
-
Distance from center point in Kilometers - take it times 1000 to match Meters of the GeoJSON variant.
By using the [core.web], Spring Data registers additional Jackson Modules
s to the ObjectMapper
for de-/serializing common Spring Data domain types.
Please refer to the [core.web.basic.jackson-mappers] 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
Note
|
The class GeoJsonConfiguration implements SpringDataJacksonModules {
@Bean
public Module geoJsonSerializers() {
return GeoJsonModule.serializers();
}
} |
Warning
|
The next major version ( |
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.
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);
-
Use the score property for sorting results by relevance which triggers
.sort({'score': {'$meta': 'textScore'}})
. -
Use
TextQuery.includeScore()
to include the calculated relevance in the resultingDocument
.
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.
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)
-
Collation
requires a locale for creation. This can be either a string representation of the locale, aLocale
(considering language, country, and variant) or aCollationLocale
. The locale is mandatory for creation. -
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.
-
Specify whether to compare numeric strings as numbers or as strings.
-
Specify whether the collation should consider whitespace and punctuation as base characters for purposes of comparison.
-
Specify whether strings with diacritics sort from back of the string, such as with some French dictionary ordering.
-
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.
Note
|
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));
Note
|
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);
Warning
|
Indexes are only used if the collation used for the operation matches the index collation. |
[mongo.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)
}
-
Static collation definition resulting in
{ 'locale' : 'en_US' }
. -
Static collation definition resulting in
{ 'locale' : 'en_US' }
. -
Dynamic collation depending on 2nd method argument. Allowed types include
String
(eg. 'en_US'),Locacle
(eg. Locacle.US) andDocument
(eg. new Document("locale", "en_US")) -
Dynamic collation depending on 2nd method argument.
-
Apply the
Collation
method parameter to the query. -
The
Collation
method parameter overrides the defaultcollation
from@Query
if not null.
Note
|
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. |
Tip
|
The most specifc Collation outroules potentially defined others. Which means Method argument over query method annotation over doamin type annotation.
|
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();
-
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();
-
The query fields are mapped against the
SWCharacter
type. -
Resulting documents are mapped into
Jedi
.
Tip
|
You can directly apply [projections] to result documents by providing the target type via as(Class<?>) .
|
Note
|
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();
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)
)
-
isEqualTo()
is an infix extension function with receiver typeKProperty<T>
that returnsCriteria
. -
For bitwise operators, pass a lambda argument where you call one of the methods of
Criteria.BitwiseCriteriaOperators
. -
To construct nested properties, use the
/
character (overloaded operatordiv
).
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)
-
The comment propagated to the MongoDB profile log.
-
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);
In pre-3.x versions of SpringData MongoDB the count operation used MongoDBs internal collection statistics.
With the introduction of [mongo.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.
Note
|
MongoDBs native Therefore a given { location : { $near : [-73.99171, 40.738868], $maxDistance : 1.1 } } (1)
{ location : { $geoWithin : { $center: [ [-73.99171, 40.738868], 1.1] } } } (2)
{ location : { $near : [-73.99171, 40.738868], $minDistance : 0.1, $maxDistance : 1.1 } } (3)
{$and :[ { $nor :[ { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 0.01] } } } ]}, { location :{ $geoWithin :{ $center :[ [-73.99171, 40.738868 ], 1.1] } } } ] } (4)
|
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.
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.
Warning
|
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)
-
Run the script directly without storing the function on server side.
-
Store the script using 'echo' as its name. The given name identifies the script and allows calling it later.
-
Run the script with name 'echo' using the provided parameters.
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.
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);
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.
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.
The MongoDB Aggregation Framework provides the following types of aggregation operations:
-
Pipeline Aggregation Operators
-
Group 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 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.
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.
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.
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.
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 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 } }
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.
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})");
The examples in this section demonstrate the usage patterns for the MongoDB Aggregation Framework with Spring Data MongoDB.
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.
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.
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.
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.
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();
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.
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"));
-
If the value of the field
author.middle
-
does not contain a value,
-
then use
$$REMOVE
to exclude the field. -
Otherwise, add the field value of
author.middle
.
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();
}
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"));
Note
|
Index and GeospatialIndex support configuration of collations.
|
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]]
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.
Note
|
Collection creation allows customization with CollectionOptions and supports collations.
|
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.
-
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.
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.
Note
|
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 .
|
Warning
|
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.
|
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.
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;
}
});
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")))
}
}
Note
|
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.