The MongoDB Shell, Unpeeled


Role of the Mongo Shell

Tips & tricks
The shell & MongoD server

The Shell in a nutshell

Your application talks to the MongoD server when it needs to read or write data. But what exactly are they doing? How is the Mongo server doing? Are the updates being committed to the server? Is the server configured correctly and performing well? All of these questions can be answered with the shell. A shell is nothing but a DOS prompt (in windows terms) like application that allows you to inspect and interrogate the server The shell uses the same wire protocol that your application would be using.

Mongo Shell Operation : Interactive & Blind

The Interactive Shell

At the command prompt you can type a Mongo command or some Java Script. You are not limited to a single line of java script or a single command, instead you can script it. The shell is a JavaScript interpreter. This means that you can script everything into a file and run the file in the shell or you can type a bunch of JavaScript  and execute it there . This opens up the possibility of creating administrative scripts, data maintenance scripts,  scripts to monitor the server health( such as monitor replication, size of collections) , exploring data etc.

Blind Shell

The shell process can also be run from the command line without actually entering the shell. It can be used to execute a JavaScript or run a command from the command line. This can be useful to run cron jobs or run batch files etc.

There are three options to use the ” Shell” (interactive & blind)

Blind “Command”

Lets say you needed to do an administrative task on a regular basis such as rotate the logs on the server when it spills over a certain size limit. You would probably want to schedule that inside a batch file. The Mongo shell allows you to do just that using the “EVAL” command line option. From the console you will run the Mongo shell using the eval command line option and the shell will execute your command line option against the Mongo server and will return the results to the console  without interfering with the Mongo interactive shell. This is important because you want it to run it as a background or scheduled task.  Now lets see the actual console command

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

In this example we are instructing Mongo to rotate the logfile (by default it will keep appending to the same file). so it will be very useful to run this using a cron job so that the file sizes can be managed.

Blind “Script”

Alternatively you could put them in a script file when there are multiple actions that need to be taken. ou would run the Mongo shell and tell it what script file to run. It will load and run the script file and then return back to the console. Again the Mongo shell will not be popped, so this can be scheduled using a batch file.

mongo  serverxx  runhourlyjob.js

Script ->Interactive

There are times when you would want to run a script but want to remain in the shell. Lets say you have a replica set where you would want to add a member, remove a member and then do something to the configuration of the replica set.  You might want to stay in the shell to make sure that the actions were completed and everything looks hunkydory.  To do that you would just add the shell command line option  and you will run the script. the Mongo shell will execute the script and instead of returning to the console it will remain in the Mongo Interactive shell so that you can evaluate the results or do something else.

mongo serverxx mydailyjob.js --shell

Tips & Tricks

Lets say we run the log rotate command. IN the command below notice that we are using the word “admin“. This is because we are issuing the command against the “admin database“. The “logRotate” command must be issued from the “admin database“. Notice that because the serverxx/admin  connection string contains the admin database, so we dont have to switch into the database within the eval command.

c:\programfiles\mongo> mongo serverxx/admin --eval "db.runCommand({logRotate:1})"
Mongo DB shell version 2.4.1
connecting to : severxx/admin
[object:object]
c:\programfiles\mongo> dir
mongo-server.log
mong-server.log.sometimeanddate

Notice that the log was rotated and Mongo will continue to log into the original file (mongo-server.log) but the message looks a bit cryptic. What does”object:object” mean ? fortunately you can use a command called printjson

[In interactive mode, mongo prints the results of operations including the content of all cursors. In scripts, either use the JavaScript print() function or the mongo specific printjson() function which returns formatted JSON.]

so we run the command again by wrapping the printjson command around the eval command

 c:\programfiles\mongo> mongo serverxx/admin --eval
"printjson(db.runCommand({logRotate:1}))"
Mongo DB shell version 2.4.1
connecting to : severxx/admin
{ OK: 1}

So Mongo was pretty much saying good job!!!

Next up MongoDB Replica Sets.

Till then CIAO!!!

For all your application development needs, visit www.verbat.com for a fiscally conscious proposal that meets your needs ( So I can keep this blog going as well!!!!)

Alternatively click through the link   if you found this article interesting. (This will help the companies Search engine rankings)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s