Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed data for a node+mongo app deployed to Heroku?

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
like image 647
June Avatar asked Aug 19 '15 18:08

June


People also ask

Can you seed to Heroku?

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.

How install MongoDB URI to Heroku?

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


1 Answers

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:

  1. Get your MONGOLAB_URI by running $ heroku config inside your app directory
  2. 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
    
like image 83
June Avatar answered Sep 30 '22 08:09

June