DATAMONGO-2427 - Polishing.
Replace leftovers to Mongo 2.x API with Document and MongoDatabase references and tweak Javadoc. Reorder field declarations to class header. Reflect 3.0 versions in schema configuration. Add TODO markers to disabled tests. Reflect changes in documentation. Enable disabled test. Original pull request: #823.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
[[new-features.3.0]]
|
||||
== What's New in Spring Data MongoDB 3.0
|
||||
|
||||
* Upgrade to MongoDB Driver 4.0. See <<upgrading.2-3>> for further details.
|
||||
* Support for <<mongo-template.aggregation-update,aggregation pipelines in update operations>>.
|
||||
* Removal of `_id` flattening for composite Id's when using `MongoTemplate` aggregations.
|
||||
* Apply pagination when using GridFS `find(Query)`.
|
||||
|
||||
@@ -39,20 +39,23 @@ The starting point for learning about MongoDB is https://www.mongodb.org/[www.mo
|
||||
[[requirements]]
|
||||
== Requirements
|
||||
|
||||
The Spring Data MongoDB 2.x binaries require JDK level 8.0 and above and https://spring.io/docs[Spring Framework] {springVersion} and above.
|
||||
The Spring Data MongoDB 3.x binaries require JDK level 8.0 and above and https://spring.io/docs[Spring Framework] {springVersion} and above.
|
||||
|
||||
In terms of document stores, you need at least version 2.6 of https://www.mongodb.org/[MongoDB].
|
||||
|
||||
[[get-started:help]]
|
||||
== Additional Help Resources
|
||||
|
||||
Learning a new framework is not always straightforward. In this section, we try to provide what we think is an easy-to-follow guide for starting with the Spring Data MongoDB module. However, if you encounter issues or you need advice, feel free to use one of the following links:
|
||||
Learning a new framework is not always straightforward.
|
||||
In this section, we try to provide what we think is an easy-to-follow guide for starting with the Spring Data MongoDB module.
|
||||
However, if you encounter issues or you need advice, feel free to use one of the following links:
|
||||
|
||||
[[get-started:help:community]]
|
||||
Community Forum :: Spring Data on https://stackoverflow.com/questions/tagged/spring-data[Stack Overflow] is a tag for all Spring Data (not just Document) users to share information and help each other. Note that registration is needed only for posting.
|
||||
Community Forum :: Spring Data on https://stackoverflow.com/questions/tagged/spring-data[Stack Overflow] is a tag for all Spring Data (not just Document) users to share information and help each other.
|
||||
Note that registration is needed only for posting.
|
||||
|
||||
[[get-started:help:professional]]
|
||||
Professional Support :: Professional, from-the-source support, with guaranteed response time, is available from https://pivotal.io/[Pivotal Sofware, Inc.], the company behind Spring Data and Spring.
|
||||
Professional Support :: Professional, from-the-source support, with guaranteed response time, is available from https://pivotal.io/[Pivotal Software, Inc.], the company behind Spring Data and Spring.
|
||||
|
||||
[[get-started:up-to-date]]
|
||||
== Following Development
|
||||
|
||||
@@ -185,10 +185,10 @@ The `MongoTransactionManager` binds a `ClientSession` to the thread. `MongoTempl
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
static class Config extends AbstractMongoConfiguration {
|
||||
static class Config extends AbstractMongoClientConfiguration {
|
||||
|
||||
@Bean
|
||||
MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { <1>
|
||||
MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) { <1>
|
||||
return new MongoTransactionManager(dbFactory);
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ static class Config extends AbstractMongoConfiguration {
|
||||
public class StateService {
|
||||
|
||||
@Transactional
|
||||
void someBusinessFunction(Step step) { <2>
|
||||
void someBusinessFunction(Step step) { <2>
|
||||
|
||||
template.insert(step);
|
||||
|
||||
|
||||
@@ -278,7 +278,6 @@ public class GeoSpatialAppConfig extends AbstractMongoClientConfiguration {
|
||||
|
||||
// the following are optional
|
||||
|
||||
|
||||
@Bean
|
||||
@Override
|
||||
public CustomConversions customConversions() throws Exception {
|
||||
@@ -311,10 +310,9 @@ Spring's MongoDB namespace lets you enable mapping functionality in XML, as the
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd
|
||||
http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<!-- Default bean name is 'mongo' -->
|
||||
@@ -769,7 +767,7 @@ To selectively handle the conversion yourself, register one or more one or more
|
||||
|
||||
NOTE: Spring 3.0 introduced a core.convert package that provides a general type conversion system. This is described in detail in the Spring reference documentation section entitled https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/core.html#validation["`Spring Type Conversion`"].
|
||||
|
||||
You can use the `customConversions` method in `AbstractMongoConfiguration` to configure converters. The examples <<mapping-configuration, at the beginning of this chapter>> show how to perform the configuration using Java and XML.
|
||||
You can use the `customConversions` method in `AbstractMongoClientConfiguration` to configure converters. The examples <<mapping-configuration, at the beginning of this chapter>> show how to perform the configuration using Java and XML.
|
||||
|
||||
The following example of a Spring Converter implementation converts from a `Document` to a `Person` POJO:
|
||||
|
||||
|
||||
@@ -13,8 +13,7 @@ This chapter coverts major changes and outlines migration steps.
|
||||
** core-jar
|
||||
** sync-jar
|
||||
|
||||
|
||||
This allows to include eg. just the reactive bits without having to pull in all the sync stuff.
|
||||
The change in dependencies allows usage of the reactive support without having to pull the synchronous driver.
|
||||
NOTE: The new sync driver does no longer support `com.mongodb.DBObject`. Please use `org.bson.Document` instead.
|
||||
|
||||
=== Signature Changes
|
||||
@@ -22,7 +21,7 @@ NOTE: The new sync driver does no longer support `com.mongodb.DBObject`. Please
|
||||
* `MongoTemplate` no longer supports `com.mongodb.MongoClient` and `com.mongodb.MongoClientOptions`.
|
||||
Please use `com.mongodb.client.MongoClient` and `com.mongodb.MongoClientSettings` instead.
|
||||
|
||||
In case you're using `AbstractMongoConfiguration` please switch to `AbstractMongoClientInformation`.
|
||||
In case you're using `AbstractMongoConfiguration` please switch to `AbstractMongoClientConfiguration`.
|
||||
|
||||
=== Namespace Changes
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
[[mongo.mongo-3]]
|
||||
= MongoDB 3.0 Support
|
||||
|
||||
Spring Data MongoDB requires MongoDB Java driver generations 3 when connecting to a MongoDB 2.6/3.0 server running MMap.v1 or a MongoDB server 3.0 using MMap.v1 or the WiredTiger storage engine.
|
||||
Spring Data MongoDB requires MongoDB Java driver generations 4 when connecting to a MongoDB 2.6/3.0 server running MMap.v1 or a MongoDB server 3.0 using MMap.v1 or the WiredTiger storage engine.
|
||||
|
||||
NOTE: See the driver- and database-specific documentation for major differences between those engines.
|
||||
|
||||
NOTE: Operations that are no longer valid when using a 3.x MongoDB Java driver have been deprecated within Spring Data and will be removed in a subsequent release.
|
||||
|
||||
== Using Spring Data MongoDB with MongoDB 3.0
|
||||
|
||||
The rest of this section describes how to use Spring Data MongoDB with MongoDB 3.0.
|
||||
@@ -51,7 +49,7 @@ MongoDB Server generation 3 changed the authentication model when connecting to
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfiguration {
|
||||
public class ApplicationContextEventTestsAppConfig extends AbstractMongoClientConfiguration {
|
||||
|
||||
@Override
|
||||
public String getDatabaseName() {
|
||||
@@ -118,11 +116,12 @@ NOTE: The field names used within the query expression are mapped to the domain
|
||||
[[mongo.mongo-3.misc]]
|
||||
=== Miscellaneous Details
|
||||
|
||||
This section covers briefly lists additional things to keep in mind when using the 3.0 driver:
|
||||
This section covers briefly lists additional things to keep in mind when using the 4.0 driver:
|
||||
|
||||
* `IndexOperations.resetIndexCache()` is no longer supported.
|
||||
* Any `MapReduceOptions.extraOption` is silently ignored.
|
||||
* `WriteResult` no longer holds error information but, instead, throws an `Exception`.
|
||||
* `MongoOperations.executeInSession(…)` no longer calls `requestStart` and `requestDone`.
|
||||
* Index name generation has become a driver-internal operation. Spring Data MongoDB still uses the 2.x schema to generate names.
|
||||
* Index name generation has become a driver-internal operation.
|
||||
Spring Data MongoDB still uses the 2.x schema to generate names.
|
||||
* Some `Exception` messages differ between the generation 2 and 3 servers as well as between the MMap.v1 and WiredTiger storage engines.
|
||||
|
||||
@@ -112,7 +112,7 @@ Because our domain repository extends `PagingAndSortingRepository`, it provides
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration
|
||||
public class PersonRepositoryTests {
|
||||
|
||||
@@ -601,7 +601,7 @@ class MongoTemplateProducer {
|
||||
@ApplicationScoped
|
||||
public MongoOperations createMongoTemplate() {
|
||||
|
||||
MongoDbFactory factory = new SimpleMongoClientDbFactory(MongoClients.create(), "database");
|
||||
MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database");
|
||||
return new MongoTemplate(factory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ There is a https://github.com/spring-projects/spring-data-examples[GitHub reposi
|
||||
[[mongodb-connectors]]
|
||||
== Connecting to MongoDB with Spring
|
||||
|
||||
One of the first tasks when using MongoDB and Spring is to create a `com.mongodb.client.MongoClient` or `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.
|
||||
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 https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/new-in-3.0.html#new-java-configuration[here] as well as the detailed documentation https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/core.html#beans-java-instantiating-container[here].
|
||||
|
||||
@@ -230,11 +230,9 @@ To use the Mongo namespace elements, you need to reference the Mongo schema, as
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
|
||||
xsi:schemaLocation=
|
||||
"http://www.springframework.org/schema/context
|
||||
https://www.springframework.org/schema/context/spring-context.xsd
|
||||
"
|
||||
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">
|
||||
@@ -281,21 +279,21 @@ The following example shows a configuration using replica sets:
|
||||
====
|
||||
|
||||
[[mongo.mongo-db-factory]]
|
||||
=== The MongoDbFactory Interface
|
||||
=== The MongoDatabaseFactory Interface
|
||||
|
||||
While `com.mongodb.client.MongoClient` is the entry point to the MongoDB driver API, connecting to a specific MongoDB database instance requires additional information, such as the database name and an optional username and password. With that information, you can obtain a `com.mongodb.client.MongoDatabase` object and access all the functionality of a specific MongoDB database instance. Spring provides the `org.springframework.data.mongodb.core.MongoDbFactory` interface, shown in the following listing, to bootstrap connectivity to the database:
|
||||
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:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
public interface MongoDbFactory {
|
||||
public interface MongoDatabaseFactory {
|
||||
|
||||
MongoDatabase getDb() throws DataAccessException;
|
||||
MongoDatabase getDatabase() throws DataAccessException;
|
||||
|
||||
MongoDatabase getDb(String dbName) 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 `MongoDbFactory` interface. In turn, you can use the `MongoDbFactory` instance to configure `MongoTemplate`.
|
||||
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:
|
||||
|
||||
@@ -307,7 +305,7 @@ public class MongoApp {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDbFactory(MongoClients.create(), "database"));
|
||||
MongoOperations mongoOps = new MongoTemplate(new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database"));
|
||||
|
||||
mongoOps.insert(new Person("Joe", 34));
|
||||
|
||||
@@ -323,17 +321,17 @@ The code in bold highlights the use of `SimpleMongoClientDbFactory` and is the o
|
||||
NOTE: Use `SimpleMongoClientDbFactory` when choosing `com.mongodb.client.MongoClient` as the entrypoint of choice.
|
||||
|
||||
[[mongo.mongo-db-factory-java]]
|
||||
=== Registering a `MongoDbFactory` Instance by Using Java-based Metadata
|
||||
=== Registering a `MongoDatabaseFactory` Instance by Using Java-based Metadata
|
||||
|
||||
To register a `MongoDbFactory` instance with the container, you write code much like what was highlighted in the previous code listing. The following listing shows a simple example:
|
||||
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:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
public class MongoConfiguration {
|
||||
|
||||
public @Bean MongoDbFactory mongoDbFactory() {
|
||||
return new SimpleMongoClientDbFactory(MongoClients.create(), "database");
|
||||
public @Bean MongoDatabaseFactory mongoDatabaseFactory() {
|
||||
return new SimpleMongoClientDatabaseFactory(MongoClients.create(), "database");
|
||||
}
|
||||
}
|
||||
----
|
||||
@@ -391,7 +389,7 @@ public class MongoClientConfiguration extends AbstractMongoClientConfiguration {
|
||||
----
|
||||
|
||||
[[mongo.mongo-db-factory-xml]]
|
||||
=== Registering a `MongoDbFactory` Instance by Using XML-based Metadata
|
||||
=== Registering a `MongoDatabaseFactory` Instance by Using XML-based Metadata
|
||||
|
||||
The `mongo` namespace provides a convenient way to create a `SimpleMongoClientDbFactory`, as compared to using the `<beans/>` namespace, as shown in the following example:
|
||||
|
||||
@@ -470,12 +468,12 @@ public class AppConfig {
|
||||
There are several overloaded constructors of `MongoTemplate`:
|
||||
|
||||
* `MongoTemplate(MongoClient mongo, String databaseName)`: Takes the `MongoClient` object and the default database name to operate against.
|
||||
* `MongoTemplate(MongoDbFactory mongoDbFactory)`: Takes a MongoDbFactory object that encapsulated the `MongoClient` object, database name, and username and password.
|
||||
* `MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter)`: Adds a `MongoConverter` to use for mapping.
|
||||
* `MongoTemplate(MongoDatabaseFactory mongoDbFactory)`: Takes a MongoDbFactory object that encapsulated the `MongoClient` object, database name, and username and password.
|
||||
* `MongoTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter mongoConverter)`: Adds a `MongoConverter` to use for mapping.
|
||||
|
||||
You can also configure a MongoTemplate by using Spring's XML <beans/> schema, as the following example shows:
|
||||
|
||||
[source,java]
|
||||
[source,xml]
|
||||
----
|
||||
<mongo:mongo-client host="localhost" port="27017"/>
|
||||
|
||||
@@ -3270,7 +3268,7 @@ boolean hasIndex = template.execute("geolocation", new CollectionCallbackBoolean
|
||||
[[gridfs]]
|
||||
== GridFS Support
|
||||
|
||||
MongoDB supports storing binary files inside its filesystem, GridFS. Spring Data MongoDB provides a `GridFsOperations` interface as well as the corresponding implementation, `GridFsTemplate`, to let you interact with the filesystem. You can set up a `GridFsTemplate` instance by handing it a `MongoDbFactory` as well as a `MongoConverter`, as the following example shows:
|
||||
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:
|
||||
|
||||
.JavaConfig setup for a GridFsTemplate
|
||||
====
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[[upgrading]]
|
||||
[[upgrading.2-3]]
|
||||
= Upgrading from 2.x to 3.x
|
||||
|
||||
Spring Data MongoDB 3.x requires the MongoDB Java Driver 4.x. +
|
||||
|
||||
Reference in New Issue
Block a user