How can I connect to mongodb from Ruby code?

how can i connect to mongodb from Ruby code?

+7
source share
3 answers

Kir's answer is suitable if you only work with Ruby. But if you are developing a Rails application, most likely you will want to connect to MongoDB with ORM, for example:

Using ORM will give you functionality. Rails developers are familiar with ActiveRecord. See the MongoDB Clients List at http://ruby-toolbox.com/ .

+4
source

First, you must install the MongoDb stone:

gem install mongo 

Then run the 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") 
+13
source

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

+3
source

All Articles