我是MongoDB的新手,并且有以下代码用于删除数据库。在删除数据库之前,我已经运行了一个命令dropAllUsersFromDatabase,用于从该数据库中删除所有用户。但是,此命令并未删除这些用户。
private final MongoClient mongo;
public void drop(final String databaseName) {
final MongoDatabase database = mongo.getDatabase(databaseName);
final BasicDBObject commandArguments = new BasicDBObject();
commandArguments.put("dropAllUsersFromDatabase", 1);
final BasicDBObject command = new BasicDBObject(commandArguments);
database.runCommand(command);
database.drop();
}
这些用户仍然留在 admin 数据库中的system.users collection,mongo存储用户身份验证和授权信息。例如:
{
"_id" : "57b3cc41851cb3aab00a3c84.57b3cc41851cb3aab00a3c84",
"user" : "57b3cc41851cb3aab00a3c84",
"db" : "57b3cc41851cb3aab00a3c84",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "8LjAxK/7z9yJLBfAchkLGg==",
"storedKey" : "HmktZ1GF+uQ8MJqIzY3/lU7VJEg=",
"serverKey" : "9CyQxvtK9BWT80la8p4o+kHyNwc="
}
},
"roles" : [
{
"role" : "dbOwner",
"db" : "57b3cc41851cb3aab00a3c84"
}
]
}
但是,如果我按如下所示执行mongo命令,它将正确删除这些用户。
db.runCommand({ dropAllUsersFromDatabase: 1});
登录授权相同,运行 database.runCommand(command);