I have a node express app with MongoDB as database. I want to have a seed.js file which I can run to fill in initial data to the database. I have no problem doing this on my local maschine. I just write the mongo commands in my seed.js file and run it with:
$ mongo localhost:27017/myApp seed.js
However when I deploy my app to Heroku including MongoLab I am not sure how to seed the data there. Google leads me to rails stuff most of the time.
So is there a simple way of seeding data for Heroku MongoLab without writing a script?
Update: If I try to run it with the MONGOLAB_URI from heroku I get an error.
$ mongo mongodb://heroku_xxxx:xxxxx.mongolab.com:xxx/heroku_xxxx seed.js
MongoDB shell version: 3.0.5
connecting to: mongodb://heroku_xxxxxxxx
2015-08-19T21:44:22.694+0100 E QUERY Error: More than one ':' detected. If this is an ipv6 address, it needs to be surrounded by '[' and ']'; heroku_xxxxxxxxxxxxx
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Are you able to seed your database locally? If so, you'll most likely be able to run the same command on heroku. You can do this directly from your local terminal ( heroku run ... ), or from a terminal inside Heroku's web GUI. The exact command will depend on which language/environment you're using.
Heroku GUI methodGo to settings. Scroll down and you will see Config Vars. Click on Reveal Config Vars. Here, put the KEY as MONGODB_URI and the VALUE as your Mongo URI string (no need to add quotes here).
Here the complete answer how to run a seed.js file with mongo commands for an app deployed to Heroku, thanks to pneumee and hunterloftis:
$ heroku config
inside your app directoryThe MONGOLAB_URI variable has the format mongodb://user:pass@host:port/db
.
Using those different parts stick together the correct terminal command to run the seed.js file:
$ mongo host:port/db -u user -p pass yourSeedFile.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With