How to list all users in the Mongo shell?

MongoDBDatabaseBig Data Analytics

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"
   ]
}
raja
Published on 29-Mar-2019 18:01:21
Advertisements