java如何获取mongodb数据库名,其中connect client在4中定义。x驱动程序
3。x驱动程序API,可以检索用户定义和当前连接的数据库名称。也就是说,我在admin下定义了一个用户,当我连接到mongodb服务器时,我可以使用以下代码(如https://mongodb.github.io/mongo-java-driver/3.12/javadoc/com/mongodb/MongoCredential.html#getSource()中定义的)检索连接用户的数据库名称:
com.mongodb.MongoClient client = new com.mongodb.MongoClient("localhost", credential, clientOptions);
String userDB = client.getCredential().getSource();
然而,我找不到相同/相似的方法来处理4。用于com的x API。mongodb。客户MongoClient(https://mongodb.github.io/mongo-java-driver/3.12/javadoc/com/mongodb/client/MongoClient.html)
是缺少功能,还是我找不到功能
提前谢谢
# 1 楼答案
4.1API中有getSource,位于:http://mongodb.github.io/mongo-java-driver/4.1/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html?is-external=true
db。getName()是一种服务器端方法https://docs.mongodb.com/manual/reference/method/db.getName/