Docs Home → Develop Applications → MongoDB Drivers → Java
Migrate from the Legacy API
On this page
Overview
In this section, you can identify the changes you need to make to migrate from the legacy API to the current API.
The legacy API, packaged as the mongodb-driver-legacy
JAR, contains
the legacy synchronous Java driver and uses naming conventions used in earlier
versions of the driver.
The current API, packaged as the mongodb-driver-sync
JAR, contains the
current synchronous Java driver. It features the MongoCollection
interface as an entry point to CRUD operations. It does not contain the
legacy API.
To perform a migration from the legacy API to the current API, ensure your code no longer references the legacy API, updating your code when necessary. Then, replace the legacy API and any uber JAR that contains it with the current API JAR in your application dependencies.
In addition to updating your application to handle any necessary changes, always check for any other differences in options and return values before moving it to production.
API Changes
The following table shows the majority of the changes in class and method names between the legacy and current API.
Legacy | Current |
---|---|
Use one of the following methods: | |
Use one of the following methods: | |
Use one of the following methods: | |
Use one of the following methods: | |
Use one of the following methods: | |
In addition to the preceding items, consider the following changes:
The current API uses
Options
classes and method chaining rather than overloaded methods.The current API uses relaxed JSON format by default in driver versions 4.0 and later. If your application relies on the strict JSON format, use the strict mode when reading or writing data. Learn how to specify the JSON format in the current API in the Document Data Format: Extended JSON guide.
The default generic type for
MongoCollection
in the current API is org.bson.Document. You can specify BasicDBObject as a type parameter if it eases your migration.In the current API, the aggregation pipeline you pass to the
aggregate()
method accepts a list of objects that extend theBson
interface whereas in the legacy API, it accepts a list of objects that extend theDBObject
interface.The method signatures also differ between the APIs. See the following API documentation for more information:
aggregate() method in the current API
aggregate() method in the legacy API
Bson interface
DBObject interface