For the latest stable version, please use Spring Data MongoDB 4.4.0!spring-doc.cn

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.spring-doc.cn

Dependency Changes

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.spring-doc.cn

Signature Changes

  • MongoTemplate no longer supports com.mongodb.MongoClient and com.mongodb.MongoClientOptions. Please use com.mongodb.client.MongoClient and com.mongodb.MongoClientSettings instead.spring-doc.cn

In case you’re using AbstractMongoConfiguration please switch to AbstractMongoClientConfiguration.spring-doc.cn

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.spring-doc.cn

<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-type="REPLICA_SET"
							   cluster-server-selection-timeout="300"
							   cluster-local-threshold="100"
							   cluster-hosts="localhost:27018,localhost:27019,localhost:27020" />
	</mongo:mongo.mongo-client>