Does MongoDB getUsers() and SHOW command fulfil the same purpose?

Both getUsers() method and show users command can be used to list all users in MongoDB, but they serve the same fundamental purpose with slightly different output formats.

Syntax

Using getUsers() method:

db.getUsers();

Using show command:

show users;

Method 1: Using getUsers()

The getUsers() method returns user information as an array of documents ?

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"
      ]
   }
]

Method 2: Using show users

The show users command displays each user as a separate document ?

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"
   ]
}

Key Differences

Aspect getUsers() show users
Output Format Array of documents Individual documents
Usage Method call with parentheses Shell command
Scriptability Better for scripts Interactive shell use

Conclusion

Both commands fulfill the same purpose of listing database users. getUsers() returns structured array output suitable for scripting, while show users provides readable individual document display for interactive use.

Updated on: 2026-03-15T00:09:02+05:30

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements