From 8a8d6716b312f562fa5cb375dc3faac4cbbec24c Mon Sep 17 00:00:00 2001 From: Patrick Collison Date: Sat, 16 Jan 2010 19:20:33 -0300 Subject: [PATCH] add a :safe option to Collection's remove() --- lib/mongo/collection.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index ffa3474..749ded3 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -240,14 +240,23 @@ module Mongo # # @example remove only documents that have expired: # users.remove({:expire => {"$lte" => Time.now}}) - def remove(selector={}) + def remove(selector={}, options={}) # 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) - @connection.send_message(Mongo::Constants::OP_DELETE, message, - "db.#{@db.name}.remove(#{selector.inspect})") + + if options[: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 -- + # only the fact that it didn't raise an error is -- so just return true + true + else + @connection.send_message(Mongo::Constants::OP_DELETE, message, + "db.#{@db.name}.remove(#{selector.inspect})") + end end # Update a single document in this collection.