我已经在连接字符串中指定了要连接的数据库,因此我希望无需硬编码即可获取数据库实例。
但是 mongoDbClient.getDatabase 方法需要数据库名称作为参数。有什么简单的方法可以做到这一点吗?
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
Run Code Online (Sandbox Code Playgroud)
我可以想到两个选择。虽然我还没有尝试过。