In the MongoDB shell, how do I list all users for the current database that I'm using?
I can't seem to find it anywhere on stackoverflow.
Asked
Active
Viewed 1.2e+01k times
51

naXa stands with Ukraine
- 35,493
- 19
- 190
- 259
4 Answers
89
You can do:
db.getUsers()
or
show users
The both commands print a list of users for the current database. See MongoDB documentation for User Management.

naXa stands with Ukraine
- 35,493
- 19
- 190
- 259
-
2> db.getUsers(); 2018-02-27T06:45:45.853+0500 E QUERY [thread1] Error: not authorized on test to execute command { usersInfo: 1.0 } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1523:1 @(shell):1:1 > show users; 2018-02-27T06:46:57.428+0500 E QUERY [thread1] Error: not authorized on test to execute command... – flik Feb 27 '18 at 01:54
-
@flik Maybe you need to reset your mongodb users – Mar 13 '18 at 19:41
-
1I'd like to leave here a comment from @flik: "It is fixed with: `show roles;` then added the role from list." – naXa stands with Ukraine Apr 23 '18 at 08:18
-
1To list users in another database: `db.getSiblingDB('admin').getUsers()`. – x-yuri Nov 08 '19 at 10:04
5
List belongs to DB users:
use db_name
and db.getUsers()
or show users
List according to the authentication type:
db.getUsers({ filter: { mechanisms: "SCRAM-SHA-256" } })
List with the credentials:
db.getUsers( {showCredentials: true, filter: { mechanisms: "SCRAM-SHA-256" }} )

Fenil Shah
- 149
- 1
- 10

Hamit YILDIRIM
- 4,224
- 1
- 32
- 35
4
If you like to list only user names and leave all other bulk info.. try
db.system.users.find({}, {"_id" : 1})

David Buck
- 3,752
- 35
- 31
- 35

Pratheep
- 81
- 4
0
Following command can be used to get lis of user on a selected database.
By using find on a selected db
use admin;
db.system.users.find({}, {"user": 1, "db": 1, "roles": 1});
To Get all the user on all the db
db.getUsers();
or
show users;
here you can get only user, db and roles nodes in response JSON.

ranjeet jha
- 1
- 3