deprecate DB#count and passing a selector to Collection#count
This commit is contained in:
parent
103224b800
commit
7b4e51e825
@ -359,10 +359,15 @@ EOS
|
||||
@db.collections_info(@name).next_object()['options']
|
||||
end
|
||||
|
||||
# Return the number of records that match +selector+. If +selector+ is
|
||||
# +nil+ or an empty hash, returns the count of all records.
|
||||
def count(selector={})
|
||||
@db.count(@name, selector || {})
|
||||
# Get the number of documents in this collection.
|
||||
#
|
||||
# Specifying a +selector+ is DEPRECATED and will be removed. Please use
|
||||
# find(selector).count() instead.
|
||||
def count(selector=nil)
|
||||
if selector
|
||||
warn "specifying a selector for Collection#count is deprecated and will be removed. Please use Collection.find(selector).count instead."
|
||||
end
|
||||
find(selector || {}).count()
|
||||
end
|
||||
|
||||
protected
|
||||
|
@ -396,17 +396,10 @@ module XGen
|
||||
}
|
||||
end
|
||||
|
||||
# Return the number of records in +collection_name+ that match
|
||||
# +selector+. If +selector+ is +nil+ or an empty hash, returns the
|
||||
# count of all records. Normally called by Collection#count.
|
||||
# DEPRECATED - use Collection.find(selector).count() instead
|
||||
def count(collection_name, selector={})
|
||||
oh = OrderedHash.new
|
||||
oh[:count] = collection_name
|
||||
oh[:query] = selector || {}
|
||||
doc = db_command(oh)
|
||||
return doc['n'].to_i if ok?(doc)
|
||||
return 0 if doc['errmsg'] == "ns missing"
|
||||
raise "Error with count command: #{doc.inspect}"
|
||||
warn "DB#count is deprecated and will be removed. Please use Collection.find(selector).count instead."
|
||||
collection(collection_name).find(selector).count()
|
||||
end
|
||||
|
||||
# Dereference a DBRef, getting the document it points to.
|
||||
|
@ -104,9 +104,12 @@ class TestCollection < Test::Unit::TestCase
|
||||
@@test.drop
|
||||
|
||||
assert_equal 0, @@test.count
|
||||
@@test.save({})
|
||||
@@test.save({})
|
||||
@@test.save("x" => 1)
|
||||
@@test.save("x" => 2)
|
||||
assert_equal 2, @@test.count
|
||||
|
||||
# TODO remove this test - it's deprecated
|
||||
assert_equal 1, @@test.count("x" => 1)
|
||||
end
|
||||
|
||||
def test_find_one
|
||||
|
@ -536,8 +536,8 @@ class DBAPITest < Test::Unit::TestCase
|
||||
@@coll.insert('a' => 3)
|
||||
|
||||
assert_equal 3, @@coll.count
|
||||
assert_equal 1, @@coll.count('$where' => Code.new('this.a > 2'))
|
||||
assert_equal 2, @@coll.count('$where' => Code.new('this.a > i', {'i' => 1}))
|
||||
assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count()
|
||||
assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count()
|
||||
end
|
||||
|
||||
def test_eval
|
||||
|
Loading…
Reference in New Issue
Block a user