MongoDB uses sh or JS files

Source: Internet
Author: User

Sometimes we need to manually insert some data into the database, but if the data format itself is more complex, and need to insert multiple data, the direct sub-mongodb Shell to write JS code like the execution of commands, although simple rough, but for more complex data situation is very inconvenient, and write JS code on the shell is not easy to modify data and so on.

So if you want to execute the JS code to write a script, need to insert, modify the data, only need to execute this script, and modify the JS code is also very convenient


For example, insert a piece of data into the test collection under the TestDB database,

Create a new Insert.js file (the file name is defined) under any path, as follows:

var url= "Mongodb://localhost:27019/testdb"; var db = connect (URL);d b.test.insert ({"Name": "User5"});


Perform:

# MONGO Insert.js MongoDB Shell version v3.4.14connecting to:mongodb://127.0.0.1:27017mongodb server Version:3.4.14conn ecting To:mongodb://localhost:27019/testdbmongodb Server version:3.4.14


View results:

Test-set:primary> Db.test.find () {"_id": ObjectId ("5ad844efa46a748c5a90fe41"), "name": "user1"} {"_id": ObjectId (" 5AD8608A8DE9DF222390480E ")," name ":" User2 "} {" _id ": ObjectId (" 5ad860998de9df222390480f ")," name ":" User3 "} {" _id ": ObjectId ("5ad860ba349d15f7f4a9ce9b"), "name": "User4"} {"_id": ObjectId ("5ad860be8c80e45b6ab87b90"), "name": "User4"  } {"_id": ObjectId ("5ad860c0049eccbc93be69b0"), "name": "User4"} {"_id": ObjectId ("5ad8611c7d1d4c1131c1a424"), "name" : "User4"} {"_id": ObjectId ("5ad8625a16805ab661da90ea"), "name": "User5"}

The data was inserted successfully.


You can also write a shell script,

Use Testdb;db.test.insert ({"Name": "User4"});

Perform:

# MONGO--port 27019 < test.sh MongoDB Shell version v3.4.14connecting To:mongodb://127.0.0.1:27019/mongodb Serv Er version:3.4.14switched to db testdbwriteresult ({"ninserted": 1}) bye


Note: The execution shell script and the JS script are different, the JS file defines the URL address of the mongdb. The shell script is to input the contents of the file into the command line. A statement such as use db_name cannot be used in a JS script.



MongoDB uses sh or JS files

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.