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
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 directory The 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 run heroku config -a yourapp
you'll see the connection string for your mongolab instance. You can then connect to that database via the same mongo
client you use locally, running the same seed.js
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.