Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversJava

Migrate from the Legacy API

On this page

  • Overview
  • API Changes

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.

The following table shows the majority of the changes in class and method names between the legacy and current API.


See the following API documentation pages for these 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 the Bson interface whereas in the legacy API, it accepts a list of objects that extend the DBObject interface.

    The method signatures also differ between the APIs. See the following API documentation for more information:

←  Upgrade Driver Versions