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?

6
  • Thanks Gopakumar for clarifying the connection. Can you clarify whether DB db object is obtainable as the legacy code's operations use DB object? Commented Jan 11, 2020 at 4: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. Commented Jan 11, 2020 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? <T> ListDatabasesIterable<T> listDatabases(Class<T> clazz) [Note: <T> is substituted by <DB>] Commented Jan 13, 2020 at 5:09
  • Example of using listDatabases() methods, you can check here and also you can see all methods in docs. Commented Jan 13, 2020 at 13:43
  • listDatabases() --> This lists database names as String. Please post an example to use <T> ListDatabasesIterable<T> listDatabases(Class<T> clazz ) so DB can be listed. Commented Jan 13, 2020 at 14:48

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.