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

1 Like

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.