This is a bit of an omission from previous articles about using MongoDB's shell to execute scripts, which is now supplemented in this blog post;
One, passing in the script file on the command line
Define a JAVASCIPRT file with the following name: Script1.js, as follows:
Print ("I am Albert Shao in the Script1.js")
Define another JavaScript file, named Script2, with the following content:
Print ("I like to read and study")
Running in the command window results in the following:
Note: The script file must be placed in the same directory file as the MONGO
If you want to run the script on MongoDB with the specified host and 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, interactive execution JS script
Iii. creating a. mongorc.js file
If some scripts are loaded frequently, you can add them to the Mongor.js file. This file will run automatically when you start the shell;
For example, we want the shell to start displaying a welcome statement. We create a file named ". Mongorc.js" in the user's home directory and add the following code to it:
var compliment = ["Like Albert", "attractive", "intellgient"];var index = Math.floor (Math.random ());p rint ("Hello, You ' re looking particularly "+ Compliment[index] +" today! ");
Expected to be directly loaded automatically, file directory:
The run will appear:
If you are not in the Administer directory, you need to specify 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