From b87e3dd3fb59906bea33150936f431b0b73d3781 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Sat, 27 Mar 2010 08:58:16 -0700 Subject: [PATCH] docs and tests for fields option with a hash --- lib/mongo/collection.rb | 6 ++++-- test/collection_test.rb | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 8382295..ec186a2 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -107,9 +107,11 @@ module Mongo # a document specifying elements which must be present for a # document to be included in the result set. # - # @option opts [Array] :fields field names that should be returned in the result + # @option opts [Array, Hash] :fields field names that should be returned in the result # set ("_id" will always be included). By limiting results to a certain subset of fields, - # you can cut down on network traffic and decoding time. + # you can cut down on network traffic and decoding time. If using a Hash, keys should be field + # names and values should be either 1 or 0, depending on whether you want to include or exclude + # the given field. # @option opts [Integer] :skip number of documents to skip from the beginning of the result set # @option opts [Integer] :limit maximum number of documents to return # @option opts [Array] :sort an array of [key, direction] pairs to sort by. Direction should diff --git a/test/collection_test.rb b/test/collection_test.rb index 952f3f0..8d00569 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -250,6 +250,25 @@ class TestCollection < Test::Unit::TestCase end end + def test_fields_as_hash + @@test.save(:a => 1, :b => 1, :c => 1) + + doc = @@test.find_one({:a => 1}, :fields => {:b => 0}) + assert_nil doc['b'] + assert doc['a'] + assert doc['c'] + + doc = @@test.find_one({:a => 1}, :fields => {:a => 1, :b => 1}) + assert_nil doc['c'] + assert doc['a'] + assert doc['b'] + + + assert_raise Mongo::OperationFailure do + @@test.find_one({:a => 1}, :fields => {:a => 1, :b => 0}) + end + end + def test_find_one id = @@test.save("hello" => "world", "foo" => "bar")