Migration Guide from 2.x to 3.x

  • 依赖项更改:替换了 mongo-java-driver,允许使用反应性支持而不提取同步驱动程序。

  • 签名更改:MongoTemplate 不再支持 MongoClientMongoClientOptions,改为使用 com.mongodb.client.MongoClientcom.mongodb.MongoClientSettings

  • 命名空间更改:对于已使用 MongoClient 的配置,需要更新配置 XML。推荐使用连接字符串提供连接信息。

Spring Data MongoDB 3.x 需要 MongoDB Java Driver 4.x。如需详细了解驱动程序版本,请访问 MongoDB Documentation

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

依赖项中的更改允许使用反应性支持,而无需提取同步驱动程序。注意:新的同步驱动程序不再支持 com.mongodb.DBObject。请改用 org.bson.Document

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.

如果你正在使用 AbstractMongoConfiguration,请切换到 AbstractMongoClientConfiguration

In case you’re using AbstractMongoConfiguration please switch to AbstractMongoClientConfiguration.

Namespace Changes

如果您有配置文件 XML,则切换到 `com.mongodb.client.MongoClient`需要更新该配置文件。提供所需连接信息最简单的方法是使用连接字符串。有关详情,请参见 MongoDB Documentation

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