How to find current user in MongoDB

In this article, we will find out the current user that I’m logged into the MongoDB Server. We can get the current user by using the command db.runCommand().

Follow the below steps:

1) Connect to the MongoDB Server:
Syntax:

mongo hostname:27017/database -u madmin -p

mongo 127.0.01:27017/test -u madmin -p

2) Execute the below query in the Mongo Shell.

db.runCommand({connectionStatus : 1}) Output:

{ "authInfo" : { "authenticatedUsers" : [ { "user" : "madmin", "db" : "test" } ], "authenticatedUserRoles" : [ { "role" : "userAdmin", "db" : "m201" } ] }, "ok" : 1 }

Where user 'madmin' is the currently logged in User to MongoDB session.

How to find current user in MongoDB

So, have explained How to find current user in MongoDB with examples. This is also solution for mongodb show current user.