Cả db.getUsers() và show users đều liệt kê người dùng của database hiện tại, nhưng định dạng đầu ra khác nhau.
Cách 1: db.getUsers()
> db.getUsers();
Trả về mảng object JavaScript - dễ xử lý trong script.
Cách 2: show users
> show users;
Lệnh shell built-in, hiển thị dạng pretty-print dễ đọc.
Ví dụ đầu ra
Giả sử database test có 2 user:
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"]
}
]
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"]
}
Kết luận: Cùng mục đích, khác định dạng. Dùng getUsers() cho scripting, show users cho interactive shell.