how can i connect to a mongodb from Ruby code ?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With