[MongoDB] use shell in Mongodb to achieve dynamic interaction with javascript

Source: Internet
Author: User
This is a bit omitted in previous articles about using mongodb shell to execute scripts. Now I will add a supplement to this blog. I. Input a script file in the command line to define a javasciprt file, name: script1.js, content: print (iamalbert tshaointhescript1.js) defines another javascript file, name

This is a bit omitted in previous articles about using mongodb shell to execute scripts. Now I will add a supplement to this blog. I. Input a script file in the command line to define a javasciprt file, name: script1.js, content: print (I am albert shao in the script1.js) defines another javascript file, name

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. <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yOe5 + 8 + fingerprint + rrNtsu/fingerprint + tcTD + 7PGo7s8L3A + fingerprint + PC9wPgo8aDI + fingerprint + pc9omj4kpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141119/2014111909274934.png" alt = "\">

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:

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.