Uninitialized constant Mongo :: Connection
I am using rails 4.1.8
together 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 tried both the combination of the code below and the one suggested by many people. But I am getting a similar error in both cases:
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
Mistake:
uninitialized constant Mongo::Connection uninitialized constant WorkdbController::MongoClient
+3
source to share
1 answer
You are using the new Mongo 2.0 driver which breaks compatibility with version 1.x. The code you are trying for the 1.x series.
You just need to do
require 'mongo'
db = Mongo::Client.new([ '192.168.56.102:27017' ], :database => 'convoos')
Here is a tutorial for the 2.x version of the Mongo Ruby driver.
+8
source to share