Migration Guide from 2.x to 3.x

Spring Data MongoDB 3.x requires the MongoDB Java Driver 4.x
To learn more about driver versions please visit the MongoDB Documentation.

Dependency Changes

  • org.mongodb:mongo-java-driver (uber jar) got replaced with:

    • bson-jar

    • core-jar

    • sync-jar

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

  • 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 AbstractMongoClientConfiguration.

Namespace Changes

The switch to com.mongodb.client.MongoClient requires an update of your configuration XML if you have one. The best way to provide required connection information is by using a connection string. Please see the MongoDB Documentation for details.

<mongo:mongo.mongo-client id="with-defaults" />
<context:property-placeholder location="classpath:..."/>

<mongo:mongo.mongo-client id="client-just-host-port"
                          host="${mongo.host}" port="${mongo.port}" />

<mongo:mongo.mongo-client id="client-using-connection-string"
                          connection-string="mongodb://${mongo.host}:${mongo.port}/?replicaSet=rs0" />
<mongo:mongo.mongo-client id="client-with-settings" replica-set="rs0">
		<mongo:client-settings cluster-connection-mode="MULTIPLE"
							   cluster-hosts="localhost:27018,localhost:27019,localhost:27020" />