updated docs / tests for safe remove

This commit is contained in:
Kyle Banker 2010-01-19 11:11:48 -05:00
parent c12a40ad3e
commit 47c0c38495
2 changed files with 24 additions and 3 deletions

View File

@ -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 --

View File

@ -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