This is a bit missing from the previous article about running scripts with MongoDB's shell, which is now supplemented in this blog.
One, passing in the script file on the command line
Define a JAVASCIPRT file with the following name: Script1.js, for example:
Print ("I am Albert Shao in the Script1.js")
The definition also has a JavaScript file, named Script2, for example:
Print ("I like to read and study")
The following results are obtained for example in the command form:
Note: The script file must be placed in the same folder file of MONGO
Assuming that you want to execute the script using the specified host and the MongoDB on port, you need to specify the address and then follow the name of the script file;
MONGO--quiet server-1:3000/foo script1.js script2.js
Second, the interactive Operation JS Script
Iii. creating a. mongorc.js file
Suppose some scripts are loaded frequently and can be added to the Mongor.js file. This file will be executed on its own when the shell is started;
For example, we want the shell to start displaying a welcome statement. We create a file named ". Mongorc.js" in the user's home folder, adding the following code to it, for example:
var compliment = ["Like Albert", "attractive", "intellgient"];var index = Math.floor (Math.random ());p rint ("Hello, You ' re looking particularly "+ Compliment[index] +" today! ");
You may be able to load the file folder directly on your own initiative:
Execution will appear:
It is assumed that the Administer folder is not available, such as the following:
Related articles:
Http://mongoblog.tumblr.com
Http://stackoverflow.com/questions/8219891/mongo-shell-simple-example-for-windows-xp
Http://www.java123.net/v/954740.html
"MongoDB" uses shell in MongoDB to implement dynamic interaction with JavaScript