Skip to content

Instantly share code, notes, and snippets.

@nol166
Created May 19, 2023 12:00
Show Gist options
  • Select an option

  • Save nol166/c5bd84b09c2030f526febd92f5334a66 to your computer and use it in GitHub Desktop.

Select an option

Save nol166/c5bd84b09c2030f526febd92f5334a66 to your computer and use it in GitHub Desktop.

Revisions

  1. nol166 created this gist May 19, 2023.
    44 changes: 44 additions & 0 deletions .mongoshrc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    // 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;
    };