如何在 Mongo shell 中列出所有用户?
要列出 Mongo shell 中的所有用户,可以使用 getUsers() 方法或 show 命令。
案例 1 − 使用 getUsers()
语法如下 −
db.getUsers();
案例 2 − 使用 show 命令
语法如下 −
show users;
让我们同时实现这两种语法,以便列出 Mongo 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" ] }
广告