Remote operation of a MongoDB database using JavaScript

Source: Internet
Author: User
Tags mongodb port number

Recently encountered in the work of this problem, in the software development process itself in the test server to create a MongoDB database, all the development work is very smooth, but to go online, you need to provide a running script to ensure that the database in the current network to create and update databases. What should I do?


Previously did not learn MongoDB, but also did not learn the script, so from Baidu and MongoDB's official website to find the answer, the original MongoDB can be executed through JavaScript commands, and MongoDB in the instructions can be in the JS script directly intact in the writing. As shown below:


conn = new Mongo ();d B = Conn.getdb ("********"); Initpayorganization ();
function Initpayorganization () {Db.payorganization.insert ({"organization": "Operator", "Enable": true});}

The next step is how to execute this JS script on a remote server, and first we need to fix how to connect to the remote database.


The command format for MongoDB connection to a remote server is as follows:

MONGO remote host IP or DNS:MONGODB port number/database name-u user-p password

The sample command code for MongoDB to connect to a remote server is as follows:

Connect MongoDB using the default port

MONGO 192.168.1.100

12 mongodb Shell version:2.4.8 CONNECTING  to : 192.168.1.100/test

Connect to MongoDB and specify ports

MONGO 192.168.1.100:27017

Connect to the specified MongoDB database

MONGO 192.168.1.100:27017/test

Specify a user name and password to connect to the specified MongoDB database

MONGO 192.168.1.200:27017/test-u user-p Password


The next step is how to run the specified script file on the remote MongoDB database, simply by adding the location of the script after the connection above.

MONGO 192.168.1.200:27017/test-u user-p Password ****.js


The JS script path here is best to take the full path.


Remote operation of a MongoDB database using 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.