MongoDB 的 getUsers() 和 SHOW 命令是否能实现同样的目的?
getUsers() 方法和 SHOW 命令都可以用来在 MongoDB shell 中列出所有用户。
案例 1 − 使用 getUsers()
语法如下 −
db.getUsers();
案例 2 − 使用 show 命令
语法如下 −
show users;
让我们实现这两个语法,以便在 MongoDB shell 中列出所有用户。
案例 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" ] }
广告