Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vtanathip/6388752 to your computer and use it in GitHub Desktop.
Save vtanathip/6388752 to your computer and use it in GitHub Desktop.
This is the way to insert initial data into MongoDB via grunt task. Thx to this plugin: https://github.com/sindresorhus/grunt-shell
//this is json array that MongoDB will easier read and import into it
//for the record if you didn't use json array MongoDB will read data per line so don't forget to re-format it
[
{ name: "Widget 1", desc: "This is Widget 1" },
{ name: "Widget 2", desc: "This is Widget 2" }
]
//I assume that you install grunt-shell
shell: {
options: { // Put this option if you want to see output when run shell script
stdout: true,
stderr: true,
failOnError: true
},
//remove exitisting database
remove_init_data: {
command: 'mongo vCommerce --eval "db.dropDatabase()"'
},
//create database/ import data from init_data.json into databse
add_init_data: {
command: 'mongoimport --db vCommerce --collection initData --type json --file server/init_data.json --jsonArray'
}
}
//grunt prepare-db
//run once use anywhere ... this task should run once when you prepare your project
//because if you run it again exitisting data you store it will deleted too.
grunt.registerTask('prepare-db', [
'shell:remove_init_data',
'shell:add_init_data'
]);
@cengizoner
Copy link

Nicely done! Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment