Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a document from a bash script to mongodb?

What is the exact code I need to execute, to insert a document into a mongodb using bash. At the moment I am able to look documents in mongodb via bash script up, but inserting does not work.

like image 972
Maximilian Avatar asked Nov 27 '15 17:11

Maximilian


1 Answers

You can inject javascript code from a javascript file:

mongo  127.0.0.1/MyDatabase script.js

with script.js:

var document = {
    name  : "document_name",
    title : "document_title"
};

db.MyCollection.insert(document);

or directly:

mongo 127.0.0.1/MyDatabase --eval 'var document = { name : "document_name", title : "document_title" }; db.MyCollection.insert(document);'
like image 118
Bertrand Martel Avatar answered Sep 21 '22 20:09

Bertrand Martel