From 7b4e51e82566d8e26f7f0881ab46cd32d4e09e3d Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Tue, 18 Aug 2009 11:48:16 -0400 Subject: [PATCH] deprecate DB#count and passing a selector to Collection#count --- lib/mongo/collection.rb | 13 +++++++++---- lib/mongo/db.rb | 13 +++---------- test/test_collection.rb | 7 +++++-- test/test_db_api.rb | 4 ++-- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index eee7756..e068dfc 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -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 diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index efe2c07..eeb1f5e 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -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. diff --git a/test/test_collection.rb b/test/test_collection.rb index d5cd4c0..6a53167 100644 --- a/test/test_collection.rb +++ b/test/test_collection.rb @@ -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 diff --git a/test/test_db_api.rb b/test/test_db_api.rb index a35d6dc..aac8bd9 100644 --- a/test/test_db_api.rb +++ b/test/test_db_api.rb @@ -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