Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am setting up quite big project based on NodeJS and MongoDB. I though about some setup script for the database so that I could automate db cleaning, setting up collections, defining indexes, etc on updates. In classic SQL approach it's common to write *.sql file that could be run from shell... Is there some good equivalent to do it with Mongo?

I added NodeJS in the title because I also though it would be nice idea to add the script definition into "scripts" part in package.json file but it is not the main problem here. Also, if it helps I decided to use Mongoose as a main driver in the project...

share|improve this question

1 Answer

up vote 2 down vote accepted

In , the equivalent to a sql file is basically a js file.

See here for more info.

And just a note: is an , not a driver.


Update

Suppose we have the following script file

/**
 * script.js
 */

db.createCollection('test');

You can execute it in mongo shell this way:

mongo 127.0.0.1:27017/dbname ./script.js

If you need to specify a username and password, you can use -u and -p switches. (Read this for more info.)

share|improve this answer
As for mongoose I know but wrote it because of hurry. As for js file, could you please provide some example of such file? Just a single collection definition (but remember that the script should work on multiple environments with different host/user/pass)... – Moby04 Sep 25 at 18:44
@Moby04 Sure, see my updated answer. – fardjad Sep 25 at 19:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.