w3resource

MongoDB: db.getUser() method

db.getUser()

The db.getUser() method is used to return user information about the specified user from a database. The user must exist on the database on which the method runs.

Syntax:

db.getUser(username)

Parameters:

Name Description Required /
Optional
Type
username The name of the user for which to retrieve information. Required string

Example: MongoDB: db.getUser() method

The following sequence of operations returns information about the mynewuser user on the test database

use test
db.getUser("mynewuser");

Output:

db.getUser("mynewuser");

     "_id" : "test.mynewuser",
     "user" : "mynewuser",
     "db" : "test",
     "roles" : [
             {
                     "role" : "read",
                     "db" : "orders"
             },
             {
                     "role" : "readWrite",
                     "db" : "test"
             },
             {
                     "role" : "dbAdmin",
                     "db" : "test"
             }
     ]

Retrieve the restaurants data from here

Required Access

To view another user’s information, you must have the viewUser action on the other user’s database.

Previous: db.revokeRolesFromUser() method
Next: db.getUsers() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/shell-methods/user-management/db-getUser.php