How to list all users in the Mongo shell?

To list all users in MongoDB shell, you can use the getUsers() method or the show users command. Both methods display user information including usernames, database assignments, roles, and authentication mechanisms.

Syntax

db.getUsers()

Or using the show command:

show users

Method 1: Using getUsers()

The getUsers() method returns all users as an array with complete user details ?

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

Method 2: Using show users

The show users command displays users in a more readable format, showing each user as a separate document ?

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

Key Differences

  • getUsers() returns users as an array structure with square brackets
  • show users displays each user as a separate document without array formatting
  • Both methods show identical user information including roles and authentication mechanisms

Conclusion

Use getUsers() for programmatic access to user data as an array, or show users for a cleaner display format. Both commands provide complete user information for the current database.

Updated on: 2026-03-15T00:14:58+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements