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

Source: Internet
Author: User

This is a bit missing from the previous article about running scripts with MongoDB's shell, which is now supplemented in this blog.

One, passing in the script file on the command line

Define a JAVASCIPRT file with the following name: Script1.js, for example:

Print ("I am Albert Shao in the Script1.js")

The definition also has a JavaScript file, named Script2, for example:

Print ("I like to read and study")

The following results are obtained for example in the command form:


Note: The script file must be placed in the same folder file of MONGO

Assuming that you want to execute the script using the specified host and the MongoDB on 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, the interactive Operation JS Script

Iii. creating a. mongorc.js file

Suppose some scripts are loaded frequently and can be added to the Mongor.js file. This file will be executed on its own when the shell is started;

For example, we want the shell to start displaying a welcome statement. We create a file named ". Mongorc.js" in the user's home folder, adding the following code to it, for example:

var compliment = ["Like Albert", "attractive", "intellgient"];var index = Math.floor (Math.random ());p rint ("Hello, You ' re looking particularly "+ Compliment[index] +"  today! ");

You may be able to load the file folder directly on your own initiative:



Execution will appear:



It is assumed that the Administer folder is not available, such as 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.