Reputation: 15785
I have already specified the database to connect in the connection string, so I hope I can get the database instance without hard coded .
But the method mongoDbClient.getDatabase needs the database name as the parameter. Is there any easy way to do that?
MongoClient mongoClient = new MongoClientURI(DispatcherConfigHolder.config.getMongoUrl());//I will put the uri in a config file so that I can change the db easily
MongoDatabase db = ...//need a MongoDataBase here
MongoCollection collection = db.getCollection("device");//so that I can access the collection from it
Upvotes: 5
Views: 1437
Reputation: 5559
With the modern API, you can use:
String uri = "mongodb+srv://...";
String databaseName = new ConnectionString(uri).getDatabase();
Database database = mongoClient.getDatabase(databaseName);
Upvotes: 3
Reputation: 325
I can think of two options. Although I haven't tried them.
Upvotes: 0