從 2.x 遷移到 3.x 的指南

Spring Data MongoDB 3.x 要求使用 MongoDB Java Driver 4.x
要了解有關驅動程式版本的更多資訊,請訪問 MongoDB 文件

依賴變更

  • org.mongodb:mongo-java-driver (uber jar) 已被替換為

    • bson-jar

    • core-jar

    • sync-jar

依賴的變更允許在無需引入同步驅動程式的情況下使用響應式支援。注意:新的同步驅動程式不再支援 com.mongodb.DBObject。請改用 org.bson.Document

簽名變更

  • MongoTemplate 不再支援 com.mongodb.MongoClientcom.mongodb.MongoClientOptions。請改用 com.mongodb.client.MongoClientcom.mongodb.MongoClientSettings

如果您正在使用 AbstractMongoConfiguration,請切換到 AbstractMongoClientConfiguration

名稱空間變更

切換到 com.mongodb.client.MongoClient 要求您更新配置 XML(如果存在)。提供所需連線資訊的最佳方式是使用連線字串。詳情請參閱 MongoDB 文件

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