updated docs / tests for safe remove
This commit is contained in:
parent
c12a40ad3e
commit
47c0c38495
|
@ -234,20 +234,28 @@ module Mongo
|
||||||
# @param [Hash] selector
|
# @param [Hash] selector
|
||||||
# If specified, only matching documents will be removed.
|
# 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:
|
# @example remove all documents from the 'users' collection:
|
||||||
# users.remove
|
# users.remove
|
||||||
# users.remove({})
|
# users.remove({})
|
||||||
#
|
#
|
||||||
# @example remove only documents that have expired:
|
# @example remove only documents that have expired:
|
||||||
# users.remove({:expire => {"$lte" => Time.now}})
|
# 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.
|
# Initial byte is 0.
|
||||||
message = ByteBuffer.new([0, 0, 0, 0])
|
message = ByteBuffer.new([0, 0, 0, 0])
|
||||||
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
message.put_array(BSON.serialize(selector, false).to_a)
|
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,
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message,
|
||||||
"db.#{@db.name}.remove(#{selector.inspect})")
|
"db.#{@db.name}.remove(#{selector.inspect})")
|
||||||
# the return value of send_message_with_safe_check isn't actually meaningful --
|
# the return value of send_message_with_safe_check isn't actually meaningful --
|
||||||
|
|
|
@ -191,6 +191,19 @@ class TestCollection < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
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
|
def test_count
|
||||||
@@test.drop
|
@@test.drop
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue