Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i connect to a mongodb from Ruby code?

how can i connect to a mongodb from Ruby code ?

like image 877
Chani Avatar asked Dec 17 '10 14:12

Chani


1 Answers

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")
like image 195
Kir Avatar answered Oct 15 '22 05:10

Kir