diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 36da522..bb51dbb 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -390,6 +390,7 @@ module XGen 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}" end diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 0c65884..b00eaa0 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -56,6 +56,11 @@ class DBAPITest < Test::Unit::TestCase assert docs.detect { |row| row['b'] == 3 } end + def test_count_on_nonexisting + @@db.drop_collection('foo') + assert_equal 0, @@db.collection('foo').count() + end + def test_find_simple @r2 = @@coll.insert('a' => 2) @r3 = @@coll.insert('b' => 3)