ทั้ง db.getUsers() และคำสั่ง show users ใช้สำหรับแสดงรายการผู้ใช้งานในฐานข้อมูลปัจจุบัน ผลลัพธ์เหมือนกันแตกต่างกันเฉพาะรูปแบบการแสดงผล
กรณีที่ 1: ใช้ db.getUsers()
db.getUsers();
> 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
show users;
> 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" ] }
สรุป: ทั้งสองคำสั่งทำหน้าที่เดียวกันคือแสดงรายการผู้ใช้งาน getUsers() คืนค่าเป็น Array ส่วน show users แสดงเป็นเอกสารต่อบรรทัด เลือกใช้ตามความสะดวก