Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB external script file

I am using mongoDB and am curious to whether you can import scripts like you can in MySQL:

mysql -uuser -ppassword database < script.sql

Can you do this with mongoDB?

Cheers

Eef

like image 707
RailsSon Avatar asked Jan 29 '10 14:01

RailsSon


People also ask

How do I run a JavaScript script in MongoDB?

Execute a JavaScript file js script in a mongo shell that connects to the test database on the mongod instance accessible via the localhost interface on port 27017 . Alternately, you can specify the mongodb connection parameters inside of the javascript file using the Mongo() constructor.

Can MongoDB be used with JavaScript?

MongoDB is a document-based NoSQL database that stores data in BSON (JSON-like) format. JavaScript is unanimously the most popular language for building web applications. It can also be used for building server-side applications usingNode. js.

What is Getsiblingdb?

A database object. Used to return another database without modifying the db variable in the shell environment.


2 Answers

You can pass a list of JS files to the mongo JavaScript shell and those will get executed:

$ echo "print('hello');" > test.js
$ ./mongo test.js
MongoDB shell version: 1.3.2-
url: test
connecting to: test
hello

You can use the normal mongo command line arguments if you need to specify a specific db, username, or password like you do in your above example.

like image 78
mdirolf Avatar answered Sep 27 '22 18:09

mdirolf


I think you need to use a library like PyMongo to access the db, but after installing you should be able to script aggainst the mongoDB very well using python.

like image 32
zoidbeck Avatar answered Sep 27 '22 17:09

zoidbeck