Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_one MongoDB Ruby Driver

Tags:

ruby

mongodb

It looks like the current Ruby Mongo Driver 2.0.4 no longer has the find_one method. I can only find it in reference to GridFS.

How can I retrieve a single document from Mongo using the official ruby driver? The find method returns a collection not a single object.

like image 728
Leo Net Avatar asked Jun 11 '15 03:06

Leo Net


1 Answers

find() returns a CollectionView which is a not-yet-executed query which behaves like an enumerable. To find just the first record:

find.limit(1).first

(Yes, it is annoying)

like image 61
Chris Heald Avatar answered Oct 26 '22 02:10

Chris Heald