Upgrading mongo-java-driver to Version 3.9.1

272 Views Asked by At

I am in the midst of upgrading the Spring version of one of our projects. As a result of this, the MongoDB library also had to be upgraded.

I am not able to track what the previous version of the MongoDB library used was, but the current version is now 3.9.1.

I have the following piece of code that doesn't work because of the upgrade, how should I rewrite it?

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;

public sampleMethod() {
  MongoClient client = null;

  if (...) {
      List<ServerAddress> saList = new ArrayList<>();
      for (...) {
          saList.add(...);
      }
      client = new MongoClient(saList);
  } else if (...) {
      MongoClientURI mongoClientURI = new MongoClientURI("mongodb://...");
      client = new MongoClient(mongoClientURI);
  } else {
      MongoClientURI mongoClientURI = new MongoClientURI("mongodb://..." + this.encryptedProperties.getProperty("mongo.username") + "....");
      client = new MongoClient(mongoClientURI);
  }

  return new MongoTemplate(client, srcDbname);
}

The problem now is with the return statement, because post Version 2.1, MongoTemplate's signature is now public MongoTemplate(com.mongodb.client.MongoClient mongoClient, String databaseName). Pre Version 2.1, it was public MongoTemplate(com.mongodb.MongoClient mongoClient, String databaseName).

I was looking at the documentation for the MongoClient interface (com.mongodb.client.MongoClient package) & it states that "Instances of this class can be created via the MongoClients factory.". Looking at MongoClients, the methods can't accommodate the creation of MongoClient with parameters List<ServerAddress>, MongoClientURI like MongoClient (com.mongodb.MongoClient package) does.

Package Old Version Link New Version Link
mongo-java-driver / com.mongodb not sure, couldn't locate but will update - 3.9.1 https://www.javadoc.io/static/org.mongodb/mongo-java-driver/3.9.1/index.html
org.springframework.data:spring-data-mongodb 2.0.8.RELEASE https://docs.spring.io/spring-data/mongodb/docs/2.0.8.RELEASE/api/ 3.2.0 https://docs.spring.io/spring-data/mongodb/docs/3.2.0/api/

Please bear with me as I am a junior dev (6 months of work experience) & I'm not familiar with upgrading project versions, thank you for your understanding

I am not sure how to proceed, I am currently reading the docs & doing some Google searches to see what a possible solution would be

I don't have experience in MongoDB

1

There are 1 best solutions below

0
java-enthusiast On

It looks like you're upgrading to version 3.2 of Spring Data MongoDB. As per the reference documentation, that major version series (3.x) requires you to use the 4.x version of the MongoDB Java Driver.

7.1. Dependency Changes Instead of the single artifact uber-jar mongo-java-driver, imports are now split to include separate artifacts:

org.mongodb:mongodb-driver-core (required)

org.mongodb:mongodb-driver-sync (optional)

org.mongodb:mongodb-driver-reactivestreams (optional)

Depending on the application one of the mongodb-driver-sync, mongodb-driver-reactivestreams artifacts is is required next to the mandatory mongodb-driver-core. It is possible to combine the sync and reactive drivers in one application if needed.