how can i connect to a mongodb from Ruby code?

前端 未结 3 2131
说谎
说谎 2021-02-08 21:41

how can i connect to a mongodb from Ruby code ?

3条回答
  •  灰色年华
    2021-02-08 22:28

    Firstly, you have to install MongoDb gem:

    gem install mongo
    

    Then run code:

    require 'rubygems'  # not necessary for Ruby 1.9
    require 'mongo'
    db = Mongo::Connection.new.db("mydb") # OR
    db = Mongo::Connection.new("localhost").db("mydb") # OR
    db = Mongo::Connection.new("localhost", 27017).db("mydb")
    

提交回复
热议问题