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