1

Here is the old code:

String uri = "mongodb://" + dbUser + ":" + dbPass + "@" + servers + "/"+dbName+ "?ssl=true&authSource=admin&connectTimeoutMS=6000&socketTimeoutMS=6000";  
MongoClientURI uriObj = new MongoClientURI(uri);            
MongoClient client = new MongoClient(uriObj);

DB db = client.getDB(dbName);  ==> deprecated  

Since MongoClient.getDB(dbName) is deprecated, here is the new code:

String uri = "mongodb://" + dbUser + ":" + dbPass + "@" + servers + "/"+dbName+ "?ssl=true&authSource=admin&connectTimeoutMS=6000&socketTimeoutMS=6000";  
MongoClientURI uriObj = new MongoClientURI(uri);            
MongoClient client = new MongoClient(uriObj);

MongoDatabase database = client.getDatabase(dbName);  ==> How to get DB db?

How to get a DB object (as DB db in the old code) from the new code?

Gopakumar Gopalan
  • 1,187
  • 14
  • 22
StackUser
  • 11
  • 3
  • Thanks Gopakumar for clarifying the connection. Can you clarify whether DB db object is obtainable as the legacy code's operations use DB object? – StackUser Jan 11 '20 at 04:41
  • Your code `DB db = client.getDB(dbName);` is for older mongodb Java drivers version like `2.14.2` and `2.13.3` See my answer below for newer. You can update your code to `mongoClient.getDatabase(dbname);`, but be careful with string-connections. – invzbl3 Jan 11 '20 at 14:14
  • MongoDatabase database = client.getDatabase(dbName); (1) Can DB db object be obtainable from either MongoDatabase database object or MongoClient client object as the legacy code uses DB's methods due to time limit to rewrite code? (2) Can DB db be obtainable from the following MongoClient method and please provide a sample code? ListDatabasesIterable listDatabases(Class clazz) [Note: is substituted by ] – StackUser Jan 13 '20 at 05:09
  • Example of using listDatabases() methods, you can check [here](https://stackoverflow.com/a/15416618/8370915) and also you can see all methods in [docs](https://mongodb.github.io/mongo-java-driver/3.11/javadoc/com/mongodb/MongoClient.html#listDatabases()). – invzbl3 Jan 13 '20 at 13:43
  • listDatabases() --> This lists database names as String. Please post an example to use ListDatabasesIterable listDatabases(Class clazz ) so DB can be listed. – StackUser Jan 13 '20 at 14:48
  • If you have more questions, you can [ask a question](https://stackoverflow.com/questions/ask) again. [More than one question per post](https://meta.stackoverflow.com/a/275909/8370915) isn't good idea. – invzbl3 Jan 19 '20 at 01:03

1 Answers1

3

Exactly your code DB db = mongoClient.getDB(dbName); is for mongodb java drivers versions like 2.14.2 and 2.13.3. Below is for newer, since 3.0.4 and 3.7.1.


As mentioned in the documentation:

Use MongoClients.create(), or MongoClient() for the legacy MongoClient API, to make a connection to a running MongoDB instance.

up to version 3.7 (Legacy MongoClient API), you can use connection string and getDatabase() method to access a database like:

MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1&ssl=true"));
MongoDatabase database = mongoClient.getDatabase("db_name");

or by the same logic:

MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1&ssl=true");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("db_name");

New MongoClient API (since 3.7)

To make a connection to a running MongoDB instance and to access a database, you can use:

MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authSource=db1&ssl=true");
MongoDatabase database = mongoClient.getDatabase("db_name");

Specify the name of the database to the getDatabase() method. If a database does not exist, MongoDB creates the database when you first store data for that database.

invzbl3
  • 5,872
  • 9
  • 36
  • 76