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