質問

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...

役に立ちましたか?

解決

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.)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top