在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.x API for com.mongodb.client.MongoClient (https://mongodb.github.io/mongo-java-driver/3.12/javadoc/com/mongodb/client/MongoClient.html),我找不到相同/类似的方法。
是它丢失了,还是我找不到那个函数。
提前谢谢。
发布于 2021-01-20 01:23:21
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/
https://stackoverflow.com/questions/65794804
复制相似问题