Sunday 6 April 2014

Run a javascript through mongo shell

Hi, we will discuss an interesting topic today. We will see how to pass and run a javascript to mongo shell. Let's create a simple js file as follows:

// script1.js
var str = "shell";
print("This is script1.js");
print("The value of the variable str is "+str);

Save the file as script1.js to any location in your system. Now we will run this js file in the mongo shell. Navigate to the bin directory of your mongo installation folder (this is required if the directory is not declared in your path variable). Type the following command:


mongo.exe --nodb d:\script1.js
 Here we simply ran the script without connecting to any mongod process (because we don't require in this case). Simple right!!!

In case you want to run multiple script in sequence, simply pass those script location in sequence as follows:

The shell will run those scripts and exit. You can use --quiet option to avoid printing the mongodb shell version.
There is one more way to run the script using the load function. Open the mongo shell and use the following command:

> load("D:\\script1.js")
This is script1.js
The value of the variable str is shell
true
>


Scripts have access to the db variable (as well as any other globals). However, shell helpers such as "use db" or "show collections" do not work from files. There are valid Java‐Script equivalents to each of these, as shown below:



use foo    ---> db.getSisterDB("foo")
show dbs  ---> db.getMongo().getDBs()
show collections ---> db.getCollectionNames()

You can also use scripts to inject variables into the shell. For example, we could have a script that simply initializes helper functions that you commonly use. The script below, for instance, may be helpful for the replication and sharding configuration. It defines a function, connectTo(), that connects to the locally-running database on the given port and sets db to that connection:

// defineConnectTo.js
/**
* Connect to a database and set db.
*/
var connectTo = function(port, dbname) {
if (!port) {
port = 27017;
}
if (!dbname) {
dbname = "test";
}
db = connect("localhost:"+port+"/"+dbname);
return db;
};



If we load this script in the shell, connectTo is now defined:


> typeof connectTo
undefined
> load('defineConnectTo.js')
> typeof connectTo
function


In addition to adding helper functions, you can use scripts to automate common tasks and administrative activities. By default, the shell will look in the directory that you started the shell in (use run("pwd") to see what directory that is). If the script is not in your current directory, you can give the shell a relative or absolute path to it.

For example, if you wanted to put your shell scripts in ~/my-scripts, you could load defineConnectTo.js with load("/home/myUser/my-scripts/defineConnectTo.js"). Note that load cannot resolve ~. You can use run() to run command-line programs from the shell. Pass arguments to the function as parameters:

> run("ls", "-l", "/home/myUser/my-scripts/")
sh70352| -rw-r--r-- 1 myUser myUser 2012-12-13 13:15 defineConnectTo.js
sh70532| -rw-r--r-- 1 myUser myUser 2013-02-22 15:10 script1.js
sh70532| -rw-r--r-- 1 myUser myUser 2013-02-22 15:12 script2.js
sh70532| -rw-r--r-- 1 myUser myUser 2013-02-22 15:13 script3.js


This is of limited use, generally, as the output is formatted oddly and it doesn’t support pipes.


<< Prev                                                                                     Next >>