[MongoDB] uses shell in Mongodb to achieve dynamic interaction with javascript. mongodbshell

Source: Internet
Author: User

[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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.