how can i connect to a mongodb from Ruby code?

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

how can i connect to a mongodb from Ruby code ?

相关标签:
3条回答
  • 2021-02-08 22:16

    Kir's answer is appropriate if you are working only with Ruby. But if you are developing a Rails app, you likely will want to connect to MongoDB with an ORM such as:

    • Mongoid
    • MongoMapper

    Using an ORM will give you the functionality Rails developers are familiar with in ActiveRecord. See a list of MongoDB Clients on http://ruby-toolbox.com/.

    0 讨论(0)
  • 2021-02-08 22:27
    • http://www.mongodb.org/display/DOCS/Ruby+Language+Center
    • http://api.mongodb.org/ruby/current/file.TUTORIAL.html

    Short version: install the Mongo gem, then db = Mongo::Connection.new.db("mydb")

    0 讨论(0)
  • 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")
    
    0 讨论(0)
提交回复
热议问题