Spring Data MongoDB 3.x 需要 MongoDB Java 驱动程序 4.x
要了解有关驱动程序版本的更多信息,请访问 MongoDB 文档Spring中文文档

依赖项更改

依赖项的更改允许使用反应式支持,而无需拉取同步驱动程序。 注意:新的同步驱动程序不再支持 。请改用。com.mongodb.DBObjectorg.bson.DocumentSpring中文文档

签名更改

  • MongoTemplate不再支持 和 。 请改用 和 。com.mongodb.MongoClientcom.mongodb.MongoClientOptionscom.mongodb.client.MongoClientcom.mongodb.MongoClientSettingsSpring中文文档

如果您正在使用,请切换到 .AbstractMongoConfigurationAbstractMongoClientConfigurationSpring中文文档

命名空间更改

切换到需要更新配置 XML(如果有)。 提供所需连接信息的最佳方式是使用连接字符串。 有关详细信息,请参阅 MongoDB 文档com.mongodb.client.MongoClientSpring中文文档

<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>