Skip to content

Instantly share code, notes, and snippets.

@stennie
Last active October 7, 2015 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stennie/3144323 to your computer and use it in GitHub Desktop.
Save stennie/3144323 to your computer and use it in GitHub Desktop.
Stennie's two-line MongoDB prompt
// 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