Syntax error How to list all users in the Mongo shell?

How to list all users in the Mongo shell?



In order to list all users in the Mongo shell, use the getUsers() method or show command.

Case 1 − Using getUsers()

The syntax is as follows −

db.getUsers();

Case 2 − Using show command

The syntax is as follows −

show users;

Let us implement both the syntaxes in order to list all users in the Mongo shell.

Case 1 − The first query is as follows −

> db.getUsers();

The following is the output −

[
   {
      "_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"
      ]
   }
]

Case 2 − The second query is as follows −

> show users;

The following is the output −

{
   "_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"
   ]
}
Updated on: 2019-07-30T22:30:25+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements