MongoDB won't connect to MongoHQ using Mongoid

前端 未结 3 1028
你的背包
你的背包 2021-01-19 19:46

I\'ve just started a brand new rails project and the first task I\'m trying to complete is creating an object and having it save within my database. I went through the auto

相关标签:
3条回答
  • 2021-01-19 20:26

    For a replica set / non-replica-set on MongoHQ:

    production:
      sessions:
        default:
          <% if ENV['MONGOHQ_HOST_LIST'] %>
          database: <%= ENV['MONGOHQ_DATABASE'] %>
          username: <%= ENV['MONGOHQ_USER'] %>
          password: <%= ENV['MONGOHQ_PASSWD'] %>
          hosts:
          <% YAML.parse(ENV['MONGOHQ_HOST_LIST']).value.each do |v| %>
            - <%= "#{v[0].value}:#{v[1].value}" %>
          <% end %>
          <% elsif ENV['MONGOHQ_URL'] %>
          uri: <%= ENV['MONGOHQ_URL'] %>
          <% end %>
          options:
            consistency: :eventual
            safe: true
    
    0 讨论(0)
  • 2021-01-19 20:48

    Database connections currently cannot be made via URI at this point in mongoid. The format of the mongoid.yml is as follows:

    environment:
      host: <%= ENV['MONGOID_HOST'] %>
      port: <%= ENV['MONGOID_PORT'] %>
      username: <%= ENV['MONGOID_USERNAME'] %>
      password: <%= ENV['MONGOID_PASSWORD'] %>
      database: <%= ENV['MONGOID_DATABASE'] %>
    

    What you will need to do is parse the MongoHQ string into its constituent parts to supply in the mongoid.yml

    If you want, someone has cooked up a gist that does this for you here

    0 讨论(0)
  • 2021-01-19 20:49

    This is how my production mongoid.yml looks like

    <% if ENV['MONGOLAB_URI'] %>
      <% uri = URI.parse(ENV['MONGOLAB_URI']) %>
    production:
      <<: *defaults
      sessions:
        default:
          <<: *default_session
          database: <%= uri.path.sub('/','') %>
          username: <%= uri.user %>
          password: <%= uri.password %>
          hosts:
            - <%= uri.host %>:<%= uri.port %>
    <% end %>
    

    and this works for me

    0 讨论(0)
提交回复
热议问题