Use of the MongoDB Shell JS (ii)

Source: Internet
Author: User

Use of the MongoDB Shell JS (ii)

The MongoDB shell is not only an interactive shell, but also can be accessed using a JS script.

Advantages and disadvantages of using JS script to interact
(1) No driver or language support is required;
(2) To facilitate Cron or manager scheduled tasks;
(3) Note: The problem is still the data format;

JS scripts are typically used to perform the following tasks
(1) Backup;
(2) dispatching Map-reduce command;
(3) Offline reporting, offline tasks;
(4) Regular tasks of the Administrator;

How to run a JS script
./mongo 127.0.0.1:3003/test–quiet Test.js
Description
(1)./mongo
MONGO client-_-;
(2) 127.0.0.1:3003/test
The ip,port of the server, and the database that needs to be connected;
(3) –quiet
Omit some header information
(4) Test.js
The script to be run, the content is:
Db.test.find (). ForEach (Printjson);

You can also use the –eval parameter
./mongo 127.0.0.1:3003/test–eval "Db.test.find (). ForEach (Printjson);
The –eval parameter is followed directly by a JS statement.

A few other notes
(1) Usually two methods are used to print the obtained results:
Print () = Normal JS
Printjson () = format JSON print, print out the format will be beautiful many
(2) The iterator (iterator command) will fail in the script operation
(3) How to know if the INSERT, delete, update and other operations are successful, please use:
Db.getlasterrorobj ()
Or
Db.getlasterror ()
(4) JS Shell related introduction See:
Https://developer.mozilla.org/En/SpiderMonkey/Introduction_to_the_JavaScript_shell

http://www.habadog.com/2011/08/09/mongodb-shell-use-js/

Use of the MongoDB Shell JS (ii)

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.