MongoDB uses the shell to enable 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:

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

Define another JavaScript file, named Script2, with the following content:

    1. 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, the interactive implementation of JS script three, create. 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:

    1. var compliment = ["Like Albert","attractive","intellgient"];
    2. var index = Math.floor (Math.random ());
    3. Print ("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:

In addition, MONGO--eval runs a script

Without entering interactive mode, run a MongoDB script directly under the command line of the OS. MONGO 127.0.0.1:27017/test--eval "Printjson (Db.users.findOne ())" Reference: http://blog.csdn.net/sxb0841901116/article/ details/41260095

MongoDB uses the shell to enable 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.