跳到主要内容
版本:4.4.3

从 2.x 迁移到 3.x 的指南

DeepSeek V3 中英对照 Migration Guide from 2.x to 3.x

Spring Data MongoDB 3.x 需要 MongoDB Java 驱动程序 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.MongoClient 和 com.mongodb.MongoClientOptions。请改用 com.mongodb.client.MongoClient 和 com.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>