Java Driver: Migrating From 4.11 to 5.0
Rate this article
The MongoDB Java driver 5.0.0 is now available!
While this version doesn't include many new features, it's removing a lot of deprecated methods and is preparing for the
future.
- Compile against the 4.11 version of the driver with deprecation warnings enabled.
- Remove deprecated classes and methods.
The behavior of the method
getElapsedTime()
was modified in the following classes:5.0.0 adds support for the
authorizedCollection
option of the listCollections
command.The
org.mongodb.scala.Observable.completeWithUnit()
method is now marked deprecated.One of the best ways to identify if your code will require any changes following the upgrade to Java Driver 5.0 is to compile against 4.11.0 with deprecation warnings enabled and remove the use of any deprecated methods and classes.
The following methods and classes have been removed in 5.0.0:
streamFactoryFactory()
method fromMongoClientSettings.Builder
getStreamFactoryFactory()
method fromMongoClientSettings
NettyStreamFactoryFactory
classNettyStreamFactory
classAsynchronousSocketChannelStreamFactory
classAsynchronousSocketChannelStreamFactoryFactory
classBufferProvider
classSocketStreamFactory
classStream
classStreamFactory
classStreamFactoryFactory
classTlsChannelStreamFactoryFactory
class
If you configure Netty using the
streamFactoryFactory()
, your code is probably like this:Now, you should use the
TransportSettings.nettyBuilder()
:In 4.11, the class
ConnectionId
was using integers.While this should have a very minor impact on your code, it's breaking binary and source compatibility. Make sure to
rebuild your binary and you should be good to go.
Three record annotations moved from:
To:
So if you are using these annotations, please make sure to update the imports and rebuild.
The first parameters of the two following builder methods in
SocketSettings
are now using a long instead of an
integer.This breaks binary compatibility but shouldn't require a code change in your code.
Filters.eqFull()
was only released in Beta
for vector search. It's now deprecated. Use Filters.eq()
instead when
instantiating a VectorSearchOptions
.The way the driver is computing the
ClusterConnectionMode
is now more consistent by using a specified replica set
name, regardless of how it's configured.In the following example, both the 4.11 and 5.0.0 drivers were returning the same
thing:
ClusterConnectionMode.MULTIPLE
.But in this example, the 4.11 driver was returning
ClusterConnectionMode.SINGLE
instead
of ClusterConnectionMode.MULTIPLE
.The behaviour of
BsonDecimal128
is now more consistent with the behaviour of Decimal128
.With the release of MongoDB Java Driver 5.0.0, it's evident that the focus has been on refining existing functionalities, removing deprecated methods, and ensuring compatibility for future enhancements. While the changes may necessitate some adjustments in your codebase, they pave the way for a more robust and efficient development experience.
Ready to upgrade? Dive into the latest version of the MongoDB Java drivers and start leveraging its enhanced capabilities today!
To finish with, don't forget to enable virtual threads in your Spring Boot 3.2.0+ projects! You just need to add this in your
application.properties
file:Got questions or itching to share your success? Head over to the MongoDB Community Forum – we're all ears and ready to help!
Top Comments in Forums
There are no comments on this article yet.