Skip to content

Commit ac78ef0

Browse files
committed
Merge pull request #600 from estolfo/drop-coll
Do not raise an error if a non-existent collection is dropped
2 parents 1e83526 + e93e556 commit ac78ef0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/mongo/collection.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def create
8484
# Drop the collection. Will also drop all indexes associated with the
8585
# collection.
8686
#
87+
# @note An error returned if the collection doesn't exist is suppressed.
88+
#
8789
# @example Drop the collection.
8890
# collection.drop
8991
#
@@ -92,6 +94,9 @@ def create
9294
# @since 2.0.0
9395
def drop
9496
database.command(:drop => name)
97+
rescue Error::OperationFailure => ex
98+
raise ex unless ex.message =~ /ns not found/
99+
false
95100
end
96101

97102
# Find documents in the collection.

spec/mongo/collection_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@
229229
it 'drops the collection from the database' do
230230
expect(database.collection_names).to_not include('specs')
231231
end
232+
233+
context 'when the collection does not exist' do
234+
235+
it 'does not raise an error' do
236+
expect(database['non-existent-coll'].drop).to be(false)
237+
end
238+
end
232239
end
233240

234241
describe '#find' do

0 commit comments

Comments
 (0)