Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uninitialized constant Mongo::Connection

Tags:

ruby

mongodb

I am using rails 4.1.8 along with ruby 2.1.2p95 and mongo-2.0.4 gem on ubuntu machine to connect from some controller (X) in rails to mongo db v3.0.3 . I have tried both the combination of code given below as suggested by many people. But i am getting the similar error in both the case:

 require 'mongo'
 include Mongo 

def mongocon
    @db = Mongo::Connection.new("192.168.56.102", 27017).db("convoos")
    #@db = MongoClient.new("192.168.56.102", 27017).db("convoos")
end 

Error :

 uninitialized constant Mongo::Connection

 uninitialized constant WorkdbController::MongoClient
like image 437
Naresh Avatar asked May 25 '15 04:05

Naresh


1 Answers

You are using the new Mongo 2.0 driver which breaks backwards compatibility with the 1.x series. The code you are trying is for the 1.x series.

You simply need to do

require 'mongo'
db = Mongo::Client.new([ '192.168.56.102:27017' ], :database => 'convoos')

Here is the tutorial for the 2.x version of the Ruby Mongo Driver.

like image 115
14 revs, 12 users 16% Avatar answered Oct 25 '22 07:10

14 revs, 12 users 16%