Thursday, April 09, 2015

View current user,host,port and database in mongo shell

                       Working would be great when there is some good interface. But when your working in the mongo shell we feel little uneasy to know the user, host, port, and database we are using at present. Even though we have certain commands to find out the current user, host, port, and database it looks like a time consuming one. Its difficult to find out them the shell. How can we make all these criteria to appear in the current shell we are working on?Okay, let's have a look at this post.
                   The normal mongo shell looks likes this with no any criteria specified:

                   The below script allows displaying the current user, host, port, and database in mongo shell. All you need to do is to paste the below-shown code into your mongo shell. 
 prompt = function() {
    user = db.runCommand({connectionStatus:1}).authInfo.authenticatedUsers[0]
    host = db.getMongo().toString().split(" ")[2]
    curDB = db.getName()
    if (user) {
       uname = user.user
    }
    else {
       uname = "local"
    }
    return uname + "@" + host + ":" + curDB + "> "
}  
                         And you can see some changes over there. So now the shell turns out like this:
 

                         The shell looks so cool now right. A simple post yet useful one.Hope this helps.




0 comments:

Post a Comment