Thursday, July 4, 2013

Setting up the MongoHQ connection

Heroku/MongoHQ supply guidance in this MongoHQ application note.  Since I didn't use the Heroku command line, I have to manually set up the environment variable.
heroku config:set MONGOHQ_URL=mongodb://[user]:[password]@[host].mongohq.com:10007/[database]
For reference, here are Heroku's notes on environment variables
I have slightly modified connect function (shown below). I supply a default to the local copy of MongoDB. I also corrected the check for db.password.nil?
def connect
  return @db if @db

  uri = 'http://localhost:27017/mydb'
  if( ENV.has_key?('MONGOHQ_URL')) then
    uri = ENV['MONGOHQ_URL']
  end

  db = URI.parse(uri)
  db_name = db.path.gsub(/^\//, '')

  @db = Mongo::Connection.new(db.host, db.port).db(db_name)
  @db.authenticate(db.user, db.password) unless (db.user.nil? || db.password.nil?)
  @db
end
When developing locally, I set the MONGOHQ_URL environment variable manually and then start rails.

No comments:

Post a Comment