"MongoDB" uses shell in MongoDB to implement dynamic interaction with JavaScript

Source: Internet
Author: User

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

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.