diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 702963d..a803312 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -98,7 +98,8 @@ module XGen # present for a document to be included in the result set OR an # instance of ObjectID to be used as the value for an _id query. # if nil an empty spec, {}, will be used. - def find_one(spec_or_object_id=nil) + # :options :: options, as passed to Collection#find + def find_one(spec_or_object_id=nil, options={}) spec = case spec_or_object_id when nil {} @@ -109,7 +110,7 @@ module XGen else raise TypeError, "spec_or_object_id must be an instance of ObjectID or Hash, or nil" end - find(spec, :limit => -1).next_object + find(spec, options.merge(:limit => -1)).next_object end # DEPRECATED - use find_one instead @@ -117,10 +118,7 @@ module XGen # Find the first record that matches +selector+. See #find. def find_first(selector={}, options={}) warn "Collection#find_first is deprecated and will be removed. Please use Collection#find_one instead." - h = options.dup - h[:limit] = 1 - cursor = find(selector, h) - cursor.next_object # don't need to explicitly close b/c of limit + find_one(selector, options) end # Save a document in this collection. diff --git a/tests/test_collection.rb b/tests/test_collection.rb index bb952dd..6745df0 100644 --- a/tests/test_collection.rb +++ b/tests/test_collection.rb @@ -88,7 +88,7 @@ class TestCollection < Test::Unit::TestCase end def test_find_one - id = @@test.save("hello" => "world") + id = @@test.save("hello" => "world", "foo" => "bar") assert_equal "world", @@test.find_one()["hello"] assert_equal @@test.find_one(id), @@test.find_one() @@ -97,6 +97,9 @@ class TestCollection < Test::Unit::TestCase assert_equal @@test.find_one("hello" => "world"), @@test.find_one() assert_equal @@test.find_one(OrderedHash["hello", "world"]), @@test.find_one() + assert @@test.find_one(nil, :fields => ["hello"]).include?("hello") + assert !@@test.find_one(nil, :fields => ["foo"]).include?("hello") + assert_equal nil, @@test.find_one("hello" => "foo") assert_equal nil, @@test.find_one(OrderedHash["hello", "foo"]) assert_equal nil, @@test.find_one(ObjectID.new)