DATAMONGO-1158 - Add Support for MongoDB Java driver 3.0.
We now support mongo-java-driver version 2.x and 3.0 along with MongoDB Server 2.6.7 and 3.0.0.
Please note that some of the configurations options might no longer be valid when used with version 3 of the MongoDB Java driver. Have a look at the table below so see some of the major differences in using version 2.x or 3.0
| 2.x | 3.0
----------------------+----------------------+-----------------------------------------------
default WriteConcern | NONE | UNACKNOWLEDGED
----------------------+----------------------+-----------------------------------------------
option for slaveOk | available | ignored
----------------------+----------------------+-----------------------------------------------
option for autoConnect| available | ignored
----------------------+----------------------+-----------------------------------------------
write result checking | available | ignored (errors are exceptions anyway)
----------------------+----------------------+-----------------------------------------------
rest index cache | available | throws UnsupportedOperationException
----------------------+----------------------+-----------------------------------------------
DBRef resolution | via DBRef.fetch | via collection.findOne
----------------------+----------------------+-----------------------------------------------
MapReduce Options | applied | ignored
----------------------+----------------------+-----------------------------------------------
authentication | via UserCredentials | via MongoClient
----------------------+----------------------+-----------------------------------------------
WriteConcernException | not available | translated to DataIntegretyViolationException
----------------------+----------------------+-----------------------------------------------
executeInSession | available | requestStart/requestDone commands ignored.
----------------------+----------------------+-----------------------------------------------
index creation | via createIndex | via createIndex
----------------------+----------------------+-----------------------------------------------
We need to soften the exception validation a bit since the message is slightly different when using different storage engines in a MongoDB 3.0 environment.
Added an explicit <mongo-client /> element and <client-options /> to the configuration schema. These elements will replace existing <mongo /> and <options /> elements in a subsequent release. Added credentials attribute to <mongo-client /> which allows to define a set of credentials used for setting up the MongoClient correctly using authentication data. We now reject <mongo-options /> configuration when using MongoDB Java driver generation 3.0 and above.
Original pull request: #273.
This commit is contained in:
committed by
Oliver Gierke
parent
909cc8b5d3
commit
57ab27aa5b
@@ -31,6 +31,7 @@ include::reference/mapping.adoc[]
|
||||
include::reference/cross-store.adoc[]
|
||||
include::reference/logging.adoc[]
|
||||
include::reference/jmx.adoc[]
|
||||
include::reference/mongo-3.adoc[]
|
||||
:leveloffset: -1
|
||||
|
||||
[[appendix]]
|
||||
|
||||
@@ -190,7 +190,7 @@ public class Person {
|
||||
----
|
||||
====
|
||||
|
||||
IMPORTANT: The `@Id` annotation tells the mapper which property you want to use for the MongoDB `_id` property and the `@Indexed` annotation tells the mapping framework to call `ensureIndex` on that property of your document, making searches faster.
|
||||
IMPORTANT: The `@Id` annotation tells the mapper which property you want to use for the MongoDB `_id` property and the `@Indexed` annotation tells the mapping framework to call `createIndex` on that property of your document, making searches faster.
|
||||
|
||||
IMPORTANT: Automatic index creation is only done for types annotated with `@Document`.
|
||||
|
||||
|
||||
93
src/main/asciidoc/reference/mongo-3.adoc
Normal file
93
src/main/asciidoc/reference/mongo-3.adoc
Normal file
@@ -0,0 +1,93 @@
|
||||
[[mongo.mongo-3]]
|
||||
= MongoDB 3.0 Support
|
||||
|
||||
Spring Data MongoDB allows usage of both _mongo-java-driver_ generations 2 and 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.
|
||||
|
||||
NOTE: Please refer to the driver and database specific documentation for major differences between those.
|
||||
|
||||
NOTE: Operations that are no longer valid using a 3.x mongo-java-driver have been deprecated within Spring Data and will be removed in a subsequent release.
|
||||
|
||||
[[mongodb:mongo-3-configuration]]
|
||||
== Using Spring Data MongoDB with MongoDB 3.0
|
||||
|
||||
=== Configuration Options
|
||||
|
||||
Some of the configuration options have been changed / removed for the _mongo-java-driver_. The following options will be ignored using the generation 3 driver:
|
||||
|
||||
* autoConnectRetry
|
||||
* maxAutoConnectRetryTime
|
||||
* slaveOk
|
||||
|
||||
Generally it is recommended to use the `<mongo:mongo-client ... />` and `<mongo:client-options ... />` elements instead of `<mongo:mongo ... />` when doing XML based configuration, since those elements will only provide you with attributes valid for the 3 generation java driver.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<?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 http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<mongo:mongo-client host="127.0.0.1" port="27017">
|
||||
<mongo:client-options write-concern="NORMAL" />
|
||||
</mongo:mongo-client>
|
||||
|
||||
</beans>
|
||||
----
|
||||
|
||||
=== WriteConcern and WriteConcernChecking
|
||||
|
||||
The `WriteConcern.NONE`, which had been used as default by Spring Data MongoDB, was removed in 3.0. Therefore in a MongoDB 3 environment the `WriteConcern` will be defaulted to `WriteConcern.UNACKNOWLEGED`. In case `WriteResultChecking.EXCEPTION` is enabled the `WriteConcern` will be altered to `WriteConcern.ACKNOWLEDGED` for write operations, as otherwise errors during execution would not be throw correctly, since simply not raised by the driver.
|
||||
|
||||
=== Authentication
|
||||
|
||||
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. Please use the `MongoClient` specific options for setting credentials via `MongoCredential` to provide authentication data.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Configuration
|
||||
public class ApplicationContextEventTestsAppConfig extends AbstractMongoConfiguration {
|
||||
|
||||
@Override
|
||||
public String getDatabaseName() {
|
||||
return "database";
|
||||
}
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
public Mongo mongo() throws Exception {
|
||||
return new MongoClient(singletonList(new ServerAddress("127.0.0.1", 27017)),
|
||||
singletonList(MongoCredential.createCredential("name", "db", "pwd".toCharArray())));
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
In order to use authentication with XML configuration use the `credentials` attribue on `<mongo-client>`.
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<?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 http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<mongo:mongo-client credentials="user:password@database" />
|
||||
|
||||
</beans>
|
||||
----
|
||||
|
||||
=== Other things to be aware of
|
||||
|
||||
This section covers additional things to keep in mind when using the 3.0 driver.
|
||||
|
||||
* `IndexOperations.resetIndexCache()` is no longer supported.
|
||||
* Any `MapReduceOptions.extraOption` is silently ignored.
|
||||
* `WriteResult` does not longer hold error informations but throws an Exception.
|
||||
* `MongoOperations.executeInSession()` no longer calls `requestStart` / `requestDone`.
|
||||
* Index name generation has become a driver internal operations, still we use the 2.x schema to generate names.
|
||||
* Some Exception messages differ between the generation 2 and 3 servers as well as between _MMap.v1_ and _WiredTiger_ storage engine.
|
||||
|
||||
|
||||
@@ -1159,7 +1159,7 @@ Before we are actually able to use full text search we have to ensure to set up
|
||||
|
||||
[source,javascript]
|
||||
----
|
||||
db.foo.ensureIndex(
|
||||
db.foo.createIndex(
|
||||
{
|
||||
title : "text",
|
||||
content : "text"
|
||||
|
||||
Reference in New Issue
Block a user