Skip to content

Instantly share code, notes, and snippets.

@nol166
Created May 19, 2023 12:00
Show Gist options
  • Save nol166/c5bd84b09c2030f526febd92f5334a66 to your computer and use it in GitHub Desktop.
Save nol166/c5bd84b09c2030f526febd92f5334a66 to your computer and use it in GitHub Desktop.
my .mongoshrc file
// default prompt
const short = false;
// shorthand for show collections and dbs
const sc = () => db.getCollectionNames();
const sd = () =>
db
.getMongo()
.getDBs()
.databases.map((db) => db.name);
// helper for feature compatibilty version
const fcv = () =>
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 });
// mongosh prompt
prompt = () => {
if (short) {
// print("Recording mode enabled")
return `${db.getName()}> `
}
let returnString = "";
const dbName = db.getName();
const isEnterprise = db.serverBuildInfo().modules.includes("enterprise");
const mongoURL = db.getMongo()._uri.includes("mongodb.net");
const nonAtlasEnterprise = isEnterprise && !mongoURL;
const usingAtlas = mongoURL && isEnterprise;
const readPref = db.getMongo().getReadPrefMode();
const isLocalHost = /localhost|127\.0\.0\.1/.test(db.getMongo()._uri);
const currentUser = db.runCommand({ connectionStatus: 1 }).authInfo
.authenticatedUsers[0]?.user;
if (usingAtlas) {
returnString += `Atlas || ${dbName} || ${currentUser} || ${readPref} || =>`;
} else if (isLocalHost) {
returnString += `${
nonAtlasEnterprise ? "Enterprise || localhost" : "localhost"
} || ${dbName} || ${readPref} || =>`;
} else if (nonAtlasEnterprise) {
returnString += `Enterprise || ${dbName} || ${currentUser} || ${readPref} || =>`;
} else {
returnString += `${dbName} || ${readPref} || =>`;
}
return returnString;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment