Connecting to two databases Mongoid

前端 未结 2 1809
感动是毒
感动是毒 2020-12-28 12:14

I have two databases that I have to use in my application. I have the following in my mongoid.yml:

development:
  # Configure available data         


        
相关标签:
2条回答
  • 2020-12-28 12:28

    In order to access a database temporarily (e.g. in a script) you can use the MongoDB Ruby drivers: Tutorials - Documentation - Low Level Documentation

    For a quick overview:

    Connect to the database:

    client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'my_db')

    db = client.database

    Query for entries via mongoDB query syntax:

    db['collection_name'].find('field_name' => 'field_value')

    0 讨论(0)
  • 2020-12-28 12:35

    hmm well can you do this seem like you messed your yaml file

    development:
      sessions:
        default:
          database: db_development
          username: my_username
          password: my_password
          hosts:
            - myserverip:27017
          options:
            consistency: :eventual
        writeable:
          database: db2_development
          username: myusername2
          password  mypassword2
          hosts:
            -  myserverip2:27018
          options:
            consistency: strong
    

    In your model just write this

    store_in session: "writeable"

    class MyModel
       include Mongoid::Document
       store_in session: "writeable"
       field :name, type: String
       field :age, type: Integer
    end
    

    FYI Never tested with password options but i guess it would work

    Hope this help

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