3

My goal is to connect with my heroku/mongolab database but I keep getting this error:

Mongo::OperationFailure at /mongotest/a/b
: need to login
file: networking.rb
location: send_message_with_gle
line: 89

The code I'm using is:

client = Mongo::MongoClient.from_uri(ENV['MONGOLAB_URI'])
db   = client.db('test')
testcoll = db['testcoll']
testcoll.insert({:'_id' => "def", :'test' => "woop de doop"})
testcoll.find()

ENV['MONGOLAB_URI']=mongodb://heroku_app########:password@ds0xxxxx.mongolab.com:xxxxx/heroku_app########

I know that the uri is correct and contains the username and password, so why the error? Also, the error occurs on the insert() line, not the line where I authenticate.

4

1 回答 1

4

Welp, turns out the url connects me to the heroku_app######## database, but I'm then trying to access the database called test so obviously I'm not authenticated. Would have been nice Mongo had returned an error specifying that I had logged in but not to the right database. Oh well.

I hadn't paid enough attention to the format of the uri, which is

mongodb://username:password@host:port/database

The database part is... pretty important, it turns out.

(I actually found the answer to this while writing the test, but if this answer had existed it might have saved me an embarrassingly large amount of time, so I'm writing it again and answering it myself.)

于 2013-03-22T03:35:02.100 回答