[MongoDB] uses shell in Mongodb to achieve dynamic interaction with javascript. mongodbshell
The use of mongodb shell to execute scripts is a bit omitted in previous articles; now I will add a supplement to this blog;
1. input the script file in the command line
Define a javasrt RT file named script1.js with the following content:
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")
Run the following command in the Command window:
Note: The script file must be stored in the same directory of mongo.
If you want to run the script on mongodb with the specified host and port, you must first specify the address and then keep up with the name of the script file;
Mongo -- quiet server-1: 3000/foo script1.js script2.js
Ii. Interactive execution of js scripts
3. Create a. mongorc. js File
If some scripts are frequently loaded, you can add them to the mongor. js file. This file will automatically run when the shell is started;
For example, we want to start shell to display a welcome statement. In the user's home directory, create a file named [. mongorc. js] and add the following code to it:
var compliment = ["like albert","attractive","intellgient"];var index = Math.floor(Math.random()*3);print("Hello, you're looking particularly " + compliment[index] + " today!");
It is expected that the file directory will be automatically loaded directly:
Run:
If the directory is not in the administer directory, You need to specify it as follows:
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