Conversion from mongodb-driver-legacy to mongodb-driver-sync

Is there any documentation that discusses converting an existing application from the mongodb-driver-legacy to the mongodb-driver-sync driver? The 4.3 driver page mentions

“The MongoDB Legacy driver mongodb-driver-legacy is the legacy synchronous Java driver whose entry point is com.mongodb.MongoClient and central classes include com.mongodb.DB , com.mongodb.DBCollection , and com.mongodb.DBCursor .”

Would like to know how these classes can be replaced/removed from the application to be able to use the sync driver.

We don’t actually have much, if any, documentation on that. It’s a good idea to create some though. Until then, here’s a quick cheat sheet:

  • MongoClientOptions → MongoClientSettings
  • new MongoClient() → MongoClients.create()
  • MongoClient.getDB() → MongoClient.getDatabase()
  • DB → MongoDatabase
  • DBCollection → MongoCollection<>
  • DBCursor → MongoCursor
  • DBCollection.findOne() → MongoCollection.find().first()
  • DBCollection.insert() → MongoCollection.insertOne() or MongoCollection.insertMany()
  • DBCollection.update() → MongoCollection.updateOne() or MongoCollection.updateMany()
  • DBCollection.remove() → MongoCollection.deleteOne() or MongoCollection.deleteMany()
  • DBCollection.count() → MongoCollection.countDocuments() or MongoCollection.estimatedDocumentCount()
  • DBCollection.findAndModify() → MongoCollection.findOneAndUpdate() or MongoCollection.findOneAndReplace() or MongoCollection.findOneAndDelete()

Couple of other points as well:

  • In general the new API prefers either Options classes or method chaining to the copious use of method overloading in the legacy API.
  • The default generic type for MongoCollection is org.bson.Document, but you can use BasicDBObject as well if that makes the port to the new API easier for you.

Hope this helps, and we’ll consider creating a documentation page for this.

Regards,
Jeff

2 Likes

Jeff,

Thanks so much for that cheat sheet. That should definitely help if we decide to do the conversion.

Steve

1 Like

Jeff going to post a few more questions regarding this next week. Positing now to keep thread alive.

Have a good weekend!

I am trying to replace the existing application with minimal changes (but obviously some of these may call for more changes), but wanted to determine replacement classes and methods for the following:

Classes:
com.mongodb.AggregationOptions;
com.mongodb.CommandResult;
com.mongodb.MapReduceCommand;
com.mongodb.MapReduceOutput;

Previous methods:
com.mongodb.DB.collectionExists
com.mongodb.DB.createCollection(String collectionName, DBObject options)

com.mongodb.DBCollection.insert(DBObject[] documents, WriteConcern writeConcern)
com.mongodb.DBCollection.setWriteConcern
com.mongodb.DBCollection.findAndRemove
com.mongodb.DBCollection.getName
com.mongodb.DBCollection.getStats

com.mongodb.DBCursor.next

Thanks

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

I feel like there’s a lot more documentation needed to migrate from the legacy API to the new driver. Connecting to a server is quite different, and the QueryBuilder class appears removed.

1 Like

After migration from mongo-java-driver 3.7.1 to mongodb-driver-sync 4.4.1 I can’t find class com.mongodb.QueryBuilder. Can you please tell me what should I use instead?

The legacy API, which includes QueryBuilder as well as DBCollection, DBCursor, etc, is included in the mongodb-driver-legacy artifact.

http://mongodb.github.io/mongo-java-driver/4.4/apidocs/mongodb-driver-legacy/index.html

Regards,
Jeff

2 Likes