MongoDB的getUsers()和SHOW命令是否达到相同的目的?
无论是getUsers()
方法和SHOW命令可用于列出在蒙戈壳的所有用户。
情况1-使用getUsers()
语法如下-
db.getUsers();
情况2-使用show命令
语法如下-
show users;
让我们实现这两种语法,以便在MongoShell中列出所有用户。
情况1-第一个查询如下-
> db.getUsers();
以下是输出-
[ { "_id" : "test.John", "user" : "John", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" }, { "role" : "dbAdmin", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }, { "_id" : "test.admin", "user" : "admin", "db" : "test", "roles" : [ { "role" : "root", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } ]
情况2-第二个查询如下-
> show users;
以下是输出-
{ "_id" : "test.John", "user" : "John", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" }, { "role" : "dbAdmin", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } { "_id" : "test.admin", "user" : "admin", "db" : "test", "roles" : [ { "role" : "root", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }