Last active
October 7, 2015 09:37
-
-
Save stennie/3144323 to your computer and use it in GitHub Desktop.
Stennie's two-line MongoDB prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stennie's two-line MongoDB prompt: | |
// | |
// [##] hh:mm:ss [myState]:[db]@[host]> | |
// MongoDB [serverversion]> | |
// | |
var cmdCount = 0; | |
var states = ["STARTUP", "PRIMARY", "SECONDARY", "RECOVERING", "FATAL", "STARTUP2", "UNKNOWN", "ARBITER", "DOWN", "ROLLBACK"]; | |
prompt = function() { | |
var now = new Date(); | |
var hours = now.getHours(); | |
var mins = now.getMinutes(); | |
var secs = now.getSeconds(); | |
// Add leading 0 to h:m:s as needed | |
var now_ts = (hours < 10 ? '0'+hours : hours) +":"+ (mins < 10 ? '0'+mins : mins) +":"+(secs < 10 ? '0'+secs : secs); | |
var host = db.serverStatus().host; | |
var version = db.serverStatus().version; | |
cmdCount++; | |
result = db.isMaster(); | |
if (result.ismaster && !result.setName) { | |
dbState = 'STANDALONE'; | |
} else if (result.secondary) { | |
dbState = 'SECONDARY'; | |
} else { | |
result = db.adminCommand({replSetGetStatus : 1}); | |
// Interpret numeric myState result | |
dbState = states[result.myState]; | |
} | |
return "\n["+cmdCount+"] "+now_ts+" "+dbState+":"+db+"@"+host+"> " + "\n" + 'MongoDB '+version+"> "; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment