add Cursor#count method

This commit is contained in:
Mike Dirolf 2009-08-18 11:26:58 -04:00
parent 256e64c4a3
commit 103224b800
2 changed files with 41 additions and 0 deletions

View File

@ -71,6 +71,20 @@ module XGen
o o
end end
# Get the size of the results set for this query.
#
# Returns the number of objects in the results set for this query. Does
# not take limit and skip into account. Raises OperationFailure on a
# database error.
def count
command = OrderedHash["count", @collection.name,
"query", @query.selector]
response = @db.db_command(command)
return response['n'].to_i if response['ok'] == 1
return 0 if response['errmsg'] == "ns missing"
raise OperationFailure, "Count failed: #{response['errmsg']}"
end
# Iterate over each object, yielding it to the given block. At most # Iterate over each object, yielding it to the given block. At most
# @num_to_return records are returned (or all of them, if # @num_to_return records are returned (or all of them, if
# @num_to_return is 0). # @num_to_return is 0).

View File

@ -31,6 +31,33 @@ class CursorTest < Test::Unit::TestCase
assert_kind_of Numeric, explaination['nscanned'] assert_kind_of Numeric, explaination['nscanned']
end end
def test_count
@@coll.clear
assert_equal 0, @@coll.find().count()
10.times do |i|
@@coll.save("x" => i)
end
assert_equal 10, @@coll.find().count()
assert_kind_of Integer, @@coll.find().count()
assert_equal 10, @@coll.find({}, :limit => 5).count()
assert_equal 10, @@coll.find({}, :offset => 5).count()
assert_equal 1, @@coll.find({"x" => 1}).count()
assert_equal 5, @@coll.find({"x" => {"$lt" => 5}}).count()
a = @@coll.find()
b = a.count()
a.each do |doc|
break
end
assert_equal b, a.count()
assert_equal 0, @@db['acollectionthatdoesn'].count()
end
def test_close_no_query_sent def test_close_no_query_sent
begin begin
cursor = @@coll.find('a' => 1) cursor = @@coll.find('a' => 1)