Leo Net
Leo Net

Reputation: 797

find_one MongoDB Ruby Driver

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.

Upvotes: 11

Views: 3984

Answers (2)

aadarshsg
aadarshsg

Reputation: 2089

You can use .find(condition).first

If you are searching by ID, you can also do

@coll = Coll.find( hash["_id"] )

Upvotes: 3

Chris Heald
Chris Heald

Reputation: 62668

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)

Upvotes: 16

Related Questions