diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 749ded3..def28b5 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -234,20 +234,28 @@ module Mongo # @param [Hash] selector # If specified, only matching documents will be removed. # + # @param opts [Boolean] :safe [false] run the operation in safe mode, which + # will call :getlasterror on the database and report any assertions. + # # @example remove all documents from the 'users' collection: # users.remove # users.remove({}) # # @example remove only documents that have expired: # users.remove({:expire => {"$lte" => Time.now}}) - def remove(selector={}, options={}) + # + # @return [True] + # + # @raise [Mongo::OperationFailure] an exception will be raised iff safe mode is enabled + # and the operation fails. + def remove(selector={}, opts={}) # Initial byte is 0. message = ByteBuffer.new([0, 0, 0, 0]) BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}") message.put_int(0) message.put_array(BSON.serialize(selector, false).to_a) - - if options[:safe] + + if opts[:safe] @connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message, "db.#{@db.name}.remove(#{selector.inspect})") # the return value of send_message_with_safe_check isn't actually meaningful -- diff --git a/test/test_collection.rb b/test/test_collection.rb index b4916ec..e71bb98 100644 --- a/test/test_collection.rb +++ b/test/test_collection.rb @@ -191,6 +191,19 @@ class TestCollection < Test::Unit::TestCase end end + def test_safe_remove + @conn = Connection.new + @db = @conn['mongo-ruby-test'] + @test = @db['test-safe-remove'] + @test.save({:a => 20}) + @conn.stubs(:receive).returns([[{'ok' => 0, 'err' => 'failed'}], 1, 0]) + + assert_raise OperationFailure do + @test.remove({}, :safe => true) + end + @test.drop + end + def test_count @@test.drop