Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Mongo Driver - Find_by_Id

What am I doing wrong here? I know the _id is in the database but I get empty result.

@b = coll.find("_id" => "4db2ebee90036f010b000001")

Thanks

like image 606
donald Avatar asked Apr 26 '11 14:04

donald


2 Answers

Use this:

coll.find(:_id => BSON::ObjectId('4db2ebee90036f010b000001')).each do |data| 
   puts data.inspect 
end
like image 77
lobster1234 Avatar answered Oct 07 '22 06:10

lobster1234


Using, Ruby version 2.3.1p112, mongo (gem) 2.4.2 and BSON (gem) 4.2.2

The following worked for me

client = Mongo::Client.new(['127.0.0.1:3001'], :database=>'dbname')
collection = client[:users]
user = collection.find({_id:'XY3h5R7aJkh5FxFhJ'}).first
like image 30
rrrrrraul Avatar answered Oct 07 '22 07:10

rrrrrraul