Skip to content

Instantly share code, notes, and snippets.

@leommoore
Last active October 11, 2017 20:47
Show Gist options
  • Save leommoore/6751107 to your computer and use it in GitHub Desktop.
Save leommoore/6751107 to your computer and use it in GitHub Desktop.
MongoDB - Mongo Shell

#MongoDB - Mongo Shell

###Single Task You can run commands in MongoDB from the command prompt by feeding the command into the mongo shell using:

mongo server1/admin --eval "db.runCommand({logRotate:1})"

mongo localhost:30000/admin --eval "db.runCommand({logRotate:1})"

This example will rotate the log file without remaining in the mongo shell. Note: admin refers to the admin database. Executing the command returns:

[object bson_object]

You can see what this contains by wrapping the command in a printjson command like this:

mongo localhost:30000/admin --eval "printjson(db.runCommand({logRotate:1}))"

Now we can see that it returns:

{ "ok" : 1 }

###Running Scripts You can run a batch of scripts using:

mongo server1 myDailyChores.js

An example of this would be if you want to run a script daily to generate stats such as the number of current users and save the information into a separate database:

var userCount = function() {

    var count = db.Users.count();
    var entry = {_id: Date(), n: count};

    db.UserCountHistory.save(entry);

    print("\nToday's User Count: " + entry.n);
};

userCount();

You can also run a script and remain in the shell using:

mongo server1 myDailyChores.js --shell

###Shell Extensions The shell has a JavaScript interpreter and you can extend the shell in numerous ways. For example you can extend the shell on your production server to stop someone from dropping a database by accident.

DB.prototype.dropdatabase = function() {
    print("Don't do it man!");
}

db.dropDatabase = DB.prototype.dropDatabase;

###Shell Editor You can specify an editor (eg nano, vim etc) to use within the shell to exit functions.

export EDITOR=nano

Or on windows

set EDITOR="c:\Program Files (x86)\Notepad++\notepad++.exe"

Then from within the mongo shell you can use:

mongo> edit userCount

This will allow you to edit using the editor and when you save it is returned to the shell.

###Running OS Commands

pwd() //Shows current directory

###Loading Scripts into Mongo Client

load('myScript.js')  //Loads a JavaScript file into the shell from the specified directory

###Default Shell Scripts You can specify files to run each time the shell loads by putting the command in the .mongorc.js file which is located in your home directory. In Windows this is in c:\users\{your username}\.mongorc.js.

You can also disable the automatic running of the .mongorc.js by specifying --norc when you invoke the shell.

mongo --norc

Sample .mongorc.js

// sample .mongorc.js script

var _no_ = function(){ print("Nope!");}

DB.prototype.dropDatabase = _no_;
db.dropDatabase = db.prototype.dropDatabase;

DB.prototype.shutdownServer = _no_;
db.shutdownServer = db.prototype.shutdownServer;

This stops you from dropping the database or shutting down the server by accident. You would need to explicitly start the mongo shell with the --norc option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment