diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index d3c9683..cf3a20c 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -66,7 +66,8 @@ module Mongo # database error. def count command = OrderedHash["count", @collection.name, - "query", @query.selector] + "query", @query.selector, + "fields", @query.fields()] response = @db.db_command(command) return response['n'].to_i if response['ok'] == 1 return 0 if response['errmsg'] == "ns missing" diff --git a/test/test_cursor.rb b/test/test_cursor.rb index 77bb4e1..f879013 100644 --- a/test/test_cursor.rb +++ b/test/test_cursor.rb @@ -220,4 +220,14 @@ class CursorTest < Test::Unit::TestCase assert_equal(by_location, @@db.db_command("cursorInfo" => 1)["byLocation_size"]) end + + def test_count_with_fields + @@coll.clear + @@coll.save("x" => 1) + + @@coll.find({}, :fields => ["a"]).each do |doc| + fail "shouldn't have any results here" + end + assert_equal(0, @@coll.find({}, :fields => ["a"]).count()) + end end