Docs Menu
Docs Home
/ / /
Java Sync Driver

Migrate from the Legacy API

On this page

  • Overview
  • Current API Benefits
  • API Changes

On this page, you can identify the changes you must make to migrate from the legacy API to the current API. You can also learn about features unique to the current Java driver and the benefits of migrating to the new 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.

You can continue to use the legacy API with each new MongoDB Server release. However, the legacy API does not support any updates introduced in MongoDB Server 3.0 or later. Additionally, the legacy API lacks support for certain features of the current Java driver. Some features only available in the non-legacy API include:

  • Change streams, a MongoDB Server feature designed to monitor real-time changes to a single collection, database, or deployment

  • Multi-document ACID transactions, which ensure atomicity of reads and writes to multiple documents and allow for transactions across multiple operations, collections, databases, documents, and shards

  • Time series collections, which store sequences of measurements over a period of time and improve query efficiency for time series data

  • Queryable Encryption, which allows you to encrypt sensitive workloads and to query the encrypted data

  • Java records, or concise Java classes that separate business logic from data representation and reduce boilerplate code

  • Native POJO support, which provides automatic or custom mapping between MongoDB documents and Java objects

To learn about more features of the current API, see What's New.

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

Legacy
Current
MongoClientOptions
MongoCollection.find().first()

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. 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:

Back

Validate Driver Artifact Signatures