title: MongoDB Show Current User
body: <p>How do I show the current user that I'm logged into the mongo shell as? This is useful to know because it is possible to change the user that you are logged in as&mdash;e.g. <code>db.auth("newuser", "password")</code>&mdash;while in the interactive shell. One can easily lose track.</p>
  
  <h3>Update</h3>
  
  <p>Using the accepted answer as a base, I changed the prompt to include user, connection, and db:</p>
  
  <p>Edit <code>.mongorc.js</code> in your home directory.</p>
  
  <pre><code>function prompt() {
      var username = "anon";
      var user = db.runCommand({connectionStatus : 1}).authInfo.authenticatedUsers[0];
      var host = db.getMongo().toString().split(" ")[2];
      var current_db = db.getName();
  
      if (!!user) {
          username = user.user;
      }
  
      return username + "@" + host + ":" + current_db + "&gt; ";
  }
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre><code>MongoDB shell version: 2.4.8
  connecting to: test
  
  anon@127.0.0.1:test&gt; use admin
  switched to db admin
  
  anon@127.0.0.1:admin&gt; db.auth("a_user", "a_password")
  1
  
  a_user@127.0.0.1:admin&gt;
  </code></pre>