1

In a mongo shell window, I'd like to periodically run a script that will display various stats on the database activity, before displaying the stats, I'd like to clear the screen. There is a "cls" command in the mongo shell, but I am not able to execute it from within the javascript.

function stats () {
while(1) {
    cls;
    print("display stats");
    sleep(5000);
}}

The line with the "cls" is not recognized.

Thank you for any suggestions, Gary

2 Answers 2

2

At the first glance it seemed that you won't be able to do it. According to the docs here: "You cannot use any shell helper (e.g. use , show dbs, etc.) inside the JavaScript file because they are not valid JavaScript.".

One option was to fill the screen with empty lines:

function clearIt () { for(var i = 0; i < 100; i++) { print() } }
clearIt()

However, thanks to @NeilLunn pointing it out there seems to be a solution:

function clearIt () { run('clear') }
clearIt()

This would execute system command which will clear your terminal screen. I don't know how reliable it is (see man clear -> depends if it can figure out how to clear screen) and this works only on POSIX systems. On Windows you would have to replace clear with cls:

function clearIt () { run('cls') }

Additional:

I looked up the source code of mongo shell (src/mongo/shell/linenoise.cpp). Here is how it clears the screen:

void linenoiseClearScreen( void ) {
#ifdef _WIN32
    COORD coord = {0, 0};
    CONSOLE_SCREEN_BUFFER_INFO inf;
    HANDLE screenHandle = GetStdHandle( STD_OUTPUT_HANDLE );
    GetConsoleScreenBufferInfo( screenHandle, &inf );
    SetConsoleCursorPosition( screenHandle, coord );
    DWORD count;
    FillConsoleOutputCharacterA( screenHandle, ' ', inf.dwSize.X * inf.dwSize.Y, coord, &count );
#else
    if ( write( 1, "\x1b[H\x1b[2J", 7 ) <= 0 ) return;
#endif
}

In case you feel like trying to implement your own screen cleaning function by filling screen with chars.

Sign up to request clarification or add additional context in comments.

2 Comments

That only applies to the listed helpers. There are some functions available that get loaded to the shell even when the script is invoked from the command line.
Yeah. But you don't need to wrap that in a function as it already is a function. Just: run('cls') . That's obviously on windows.
0
> help admin
    ls([path])                      list files
    pwd()                           returns current directory
    listFiles([path])               returns file list
    hostname()                      returns name of this host
    cat(fname)                      returns contents of text file as a string
    removeFile(f)                   delete a file or directory
    load(jsfilename)                load and execute a .js file
    run(program[, args...])         spawn a program and wait for its completion
    runProgram(program[, args...])  same as run(), above
    sleep(m)                        sleep m milliseconds
    getMemInfo()                    diagnostic

This shows the run and runProgram commands along with some other helpers. The program argument is a string.

2 Comments

Which one of these does the job?
As mentioned in the answer there are run and runProgram. All of these are loaded javascript functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.