Overview
In this section, you can identify the changes you must make to your application to upgrade your driver to a new version.
Before you upgrade, perform the following actions:
Ensure the new driver version is compatible with the MongoDB Server versions your application connects to and the Java Runtime Environment (JRE) your application runs on. To view compatibility information, see the Compatibility page.
Address any breaking changes between the current version of the driver your application is using and your planned upgrade version in the Breaking Changes section. To learn more about MongoDB Server release compatibility changes, see the Server Release Compatibility Changes section.
Tip
To minimize the number of changes your application might require when you upgrade driver versions in the future, use the Stable API.
Breaking Changes
A breaking change is a modification in a convention or behavior in a specific version of the driver that might prevent your application from working properly if not addressed before upgrading.
The breaking changes in this section are categorized by the driver version that introduced them. When upgrading driver versions, address all the breaking changes between the current and upgrade versions. For example, if you are upgrading the driver from v5.0 to v5.5, address all breaking changes from the versions after v5.0 including any listed for v5.5.
Version 5.5 Breaking Changes
The driver is no longer compatible with MongoDB Server version 4.0. To learn more about this change, see the Driver Version 5.5 Server Support Changes section.
Version 5.2 Breaking Changes
This driver version introduces the following breaking changes:
Drops support for MongoDB Server v3.6. To learn more about this change, see the Driver Version 5.2 Server Support Changes section.
Revises the mongodb-crypt dependency versioning to match the versioning for the JVM drivers. Future versions of
mongodb-cryptare released alongside the driver and share the same version number. You must upgrade yourmongodb-cryptdependency to v5.2.0 when upgrading your driver for this release.
Version 5.1 Breaking Changes
This driver version introduces the following breaking changes:
When using the
MONGODB-OIDCauthentication mechanism, you cannot include comma characters in theauthMechanismPropertiesconnection string value. If yourauthMechanismPropertiesvalue includes a comma, pass the value as a connection option in aMongoClientSettingsinstance.
Version 5.0 Breaking Changes
This driver version introduces the following breaking changes:
Introduces the following changes to the
ConnectionIdclass:The
ConnectionIdconstructor now accepts a value of typelongas its second parameter instead of typeint. Similarly, the constructor now accepts a value of typeLongas its third parameter instead of typeInteger. Because this change breaks binary compatibility, recompile any existing code that calls theConnectionIdconstructor.The
withServerValue()method now accepts a parameter of typelongrather than typeint. Because this change breaks binary compatibility, you must recompile any code that calls thewithServerValue()method.The
getServerValue()method now returns a value of typeLonginstead of typeInteger. Similarly, thegetLocalValue()method returns a value of typelonginstead of typeint. Because this change breaks both binary and source compatibility, update any source code that uses these methods and rebuild your binary.
Replaces the following record annotations from the
org.bson.codecs.record.annotationspackage with annotations of the same name from theorg.bson.codecs.pojo.annotationspackage:BsonIdBsonPropertyBsonRepresentation
Changes the data type of the
connectTimeouttimeout duration parameter for theSocketSettings.Builder.connectTimeout()andSocketSettings.Builder.readTimeout()methods. The data type of this parameter is nowlonginstead ofint.In earlier versions, this parameter is of type
intfor both methods. This change breaks binary compatibility and requires recompiling, but does not require code changes.Removes the
Filters.eqFull()method, released exclusively inBeta, which allowed you to construct an equality filter when performing a vector search. Instead, you can use theFilters.eq()method when instantiating aVectorSearchOptionstype, as shown in the following code:val opts = vectorSearchOptions().filter(eq("x", 8)) Changes how
ClusterSettingscomputes theClusterConnectionModesetting, making it more consistent by using the specified replica set name, regardless of how it is configured. Previously, the driver considered the replica set name only if it was set in the connection string.For example, the following two code samples both return the value
ClusterConnectionMode.MULTIPLE. Previously, the second example returnedClusterConnectionMode.SINGLE.ClusterSettings.builder() .applyConnectionString(ConnectionString("mongodb://127.0.0.1:27017/?replicaSet=replset")) .build() .mode ClusterSettings.builder() .hosts(listOf(ServerAddress("127.0.0.1", 27017))) .requiredReplicaSetName("replset") .build() .mode BsonDecimal128values respond to method calls in the same way asDecimal128values.BsonDecimal128.isNumber()now returnstrueandBsonDecimal128.asNumber()returns the equivalentBsonNumber.Removes the ServerAddress methods
getSocketAddress()andgetSocketAddresses().Instead of
getSocketAddress(), use thegetByName()instance method ofjava.net.InetAddress.Instead of
getSocketAddresses(), use thegetAllByName()instance method ofjava.net.InetAddress.Removes the UnixServerAddress methods
getSocketAddress()andgetUnixSocketAddress().Instead of
getSocketAddress(), use thegetByName()instance method ofjava.net.InetAddress.Instead of
getUnixSocketAddress(), construct an instance ofjnr.unixsocket.UnixSocketAddress. Pass the full path of the UNIX socket file to the constructor. By default, MongoDB creates a UNIX socket file located at"/tmp/mongodb-27017.sock". To learn more about theUnixSocketAddressclass, see the UnixSocketAddress API documentation.Removes the
Parameterizableinterface. Instead of implementing this interface on a customCodectype, override theCodecProvider.get()method on the codec'sCodecProviderif the codec is intended for a parameterized type.Removes the
isSlaveOk()method from theReadPreferenceandTaggableReadPreferenceclasses. To check whether a read preference allows reading from a secondary member of a replica set, use theisSecondaryOk()methods from these classes instead.Removes the
DBCollection.getStats()andDBCollection.isCapped()helper methods for thecollStatscommand. Instead of these methods, you can use the$collStatsaggregation pipeline stage.Removes the
MapCodecandIterableCodecclasses. Instead ofMapCodec, useMapCodecProvider. Instead ofIterableCodec, useCollectionCodecProvider, orIterableCodecProviderforIterabletypes that aren'tCollectiontypes.Removes the
sharded()andnonAtomic()methods from theMapReducePublisherandMapReduceIterableclasses.Removes the following methods for use with
geoHaystackindexes:Indexes.geoHaystack()IndexOptions.getBucketSize()IndexOptions.bucketSize()
Instead, you can use the
$geoNearaggregation pipeline stage or a geospatial query operator on a 2d index. For more information, see the Geospatial Queries page in the MongoDB Server manual.Removes the
oplogReplayoption from find operations. The followingoplogReplaymethods are no longer available:DBCursor.oplogReplay()DBCollectionFindOptions.isOplogReplay()DBCollectionFindOptions.oplogReplay()FindPublisher.oplogReplay()FindIterable.oplogReplay()
Removes the following
Exceptionconstructors:MongoBulkWriteException(BulkWriteResult, List<BulkWriteError>, WriteConcernError, ServerAddress)MongoCursorNotFoundException(long, ServerAddress)MongoQueryException(ServerAddress, int, String)MongoQueryException(ServerAddress, int, String, String)MongoQueryException(MongoCommandException)
Removes the following overloads for the
BulkWriteResult.acknowledged()method:acknowledged(Type, int, List<BulkWriteUpsert>)acknowledged(Type, int, Integer, List<BulkWriteUpsert>)acknowledged(int, int, int, Integer, List<BulkWriteUpsert>)
Removes the following
ChangeStreamDocumentconstructors:ChangeStreamDocument(String, BsonDocument, BsonDocument, BsonDocument, TDocument, TDocument, BsonDocument, ...)ChangeStreamDocument(String, BsonDocument, BsonDocument, BsonDocument, TDocument, BsonDocument, BsonTimestamp, ...)ChangeStreamDocument(OperationType, BsonDocument, BsonDocument, BsonDocument, TDocument, BsonDocument, BsonTimestamp, ...)
Removes the following constructors for events:
CommandEvent(RequestContext, int, ConnectionDescription, String)CommandEvent(int, ConnectionDescription, String)CommandEvent(RequestContext, long, int, ConnectionDescription, String)CommandFailedEvent(RequestContext, int, ConnectionDescription, String, long, Throwable)CommandFailedEvent(int, ConnectionDescription, String, long, Throwable)CommandStartedEvent(RequestContext, int, ConnectionDescription, String, String, BsonDocument)CommandStartedEvent(int, ConnectionDescription, String, String, BsonDocument)CommandSucceededEvent(RequestContext, int, ConnectionDescription, String, BsonDocument, long)CommandSucceededEvent(int, ConnectionDescription, String, BsonDocument, long)ConnectionCheckedInEvent(ConnectionId)ConnectionCheckedOutEvent(ConnectionId, long)ConnectionCheckedOutEvent(ConnectionId)ConnectionCheckOutFailedEvent(ServerId, long, Reason)ConnectionCheckOutFailedEvent(ServerId, Reason)ConnectionCheckOutStartedEvent(ServerId)ConnectionReadyEvent(ConnectionId)ServerHeartbeatFailedEvent(ConnectionId, long, Throwable)ServerHeartbeatSucceededEvent(ConnectionId, BsonDocument, long)
Removes the
errorLabelsoption from theWriteConcernErrorclass. This includes theaddLabel()andgetErrorLabels()methods and the constructor that includes anerrorLabelsparameter. Instead, you can use the error labels included in theMongoExceptionobject that contains theWriteConcernError.Removes the following classes from the
com.mongodb.eventpackage:ConnectionAddedEventConnectionPoolOpenedEventConnectionRemovedEventClusterListenerAdapterConnectionPoolListenerAdapterServerListenerAdapterServerMonitorListenerAdapter
The driver also removes the following related methods from the
ConnectionPoolListenerinterface:connectionAdded()connectionPoolOpened()connectionRemoved()
For more information about the
com.mongodb.eventpackage, see the API documentation.
Adds the
authorizedCollectionoption for thelistCollectionscommand. This introduces a breaking binary change in theMongoDatabase.listCollectionNames()method. This change does not require any changes to source code, but you must recompile any code that uses this method.Removes the following methods and types related to the Stream interface:
MongoClientSettings.Builder.streamFactoryFactory()method. Use theMongoClientSettings.Builder.transportSettings()method instead.MongoClientSettings.getStreamFactoryFactory()method. Use theMongoClientSettings.getTransportSettings()method instead.NettyStreamFactoryFactoryclass. Instead, call theTransportSettings.nettyBuilder()method to create aNettyTransportSettingsobject. Then, call theMongoClientSettings.Builder.transportSettings()method to apply the settings.NettyStreamFactoryclass.AsynchronousSocketChannelStreamFactoryclass.AsynchronousSocketChannelStreamFactoryFactoryclass.BufferProviderinterface.SocketStreamFactoryclass.Streaminterface.StreamFactoryinterface.StreamFactoryFactoryinterface.TlsChannelStreamFactoryFactoryclass.
Tip
To view breaking changes for earlier driver versions, see the
Release Notes in the
mongo-java-driver GitHub repository.
Server Release Compatibility Changes
A server release compatibility change is a modification to the Kotlin Sync driver that discontinues support for a set of MongoDB Server versions.
The driver discontinues support for a MongoDB Server version after it reaches end-of-life (EOL).
To learn more about the MongoDB support for EOL products, see the Legacy Support Policy.
Server Version 8.1 Support Changes
You cannot use a 3.x version of the Kotlin Sync driver to connect to a
MongoDB deployment running MongoDB Server v8.1 or later. Starting in MongoDB Server v8.1,
the buildinfo command requires authentication, causing an
incompatibility with the v3.x driver.
Driver Version 5.5 Server Support Changes
The v5.5 driver drops support for MongoDB Server 4.0. To use the v5.5 driver, you must connect to MongoDB Server 4.2 or later. To learn how to upgrade your MongoDB Server deployment, see Release Notes in the MongoDB Server manual.
Driver Version 5.2 Server Support Changes
The v5.2 driver drops support for MongoDB Server 3.6. To use the v5.2 driver, you must connect to MongoDB Server 4.0 or later. To learn how to upgrade your MongoDB Server deployment, see Release Notes in the MongoDB Server manual.
Driver Version 4.8 Server Support Changes
The v4.8 driver drops support for MongoDB Server 3.4 and earlier. To use the v4.8 driver, you must connect to MongoDB Server 3.6 or later. To learn how to upgrade your MongoDB Server deployment, see Release Notes in the MongoDB Server manual.