5

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
June
  • 385
  • 5
  • 14
  • 3
    The mongodb://user:pass@host:port/db URI format does not work for the mongo shell. You'll need to break it up: mongo host:port/db -u user -p pass – pneumee Aug 19 '15 at 22:17

2 Answers2

5

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
    
June
  • 385
  • 5
  • 14
0

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.

hunterloftis
  • 13,386
  • 5
  • 48
  • 50
  • Thanks. I understand your solution and it was exactly what I was looking for. Unfortunately I get an error when I do that and I don't understand the fix suggested in the error message. I included it in the question. – June Aug 19 '15 at 20:55