Skip to content

Commit a8ef8fc

Browse files
author
Mike Dirolf
committed
accept any type for remove by _id, not just ObjectId
1 parent 98d91d0 commit a8ef8fc

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

pymongo/collection.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -314,60 +314,54 @@ def update(self, spec, document,
314314
message.update(self.__full_name, upsert, multi,
315315
spec, document, safe), safe)
316316

317-
def remove(self, spec_or_object_id=None, safe=False):
317+
def remove(self, spec_or_id=None, safe=False):
318318
"""Remove a document(s) from this collection.
319319
320320
.. warning:: Calls to :meth:`remove` should be performed with
321321
care, as removed data cannot be restored.
322322
323-
Raises :class:`~pymongo.errors.TypeError` if
324-
`spec_or_object_id` is not an instance of (``dict``,
325-
:class:`~pymongo.objectid.ObjectId`). If `safe` is ``True``
326-
then the remove operation will be checked for errors, raising
323+
If `safe` is ``True`` then the remove operation will be
324+
checked for errors, raising
327325
:class:`~pymongo.errors.OperationFailure` if one
328326
occurred. Safe removes wait for a response from the database,
329327
while normal removes do not.
330328
331-
If no `spec_or_object_id` is given all documents in this
332-
collection will be removed. This is not equivalent to calling
333-
:meth:`~pymongo.database.Database.drop_collection`, however, as
334-
indexes will not be removed.
329+
If `spec_or_id` is ``None``, all documents in this collection
330+
will be removed. This is not equivalent to calling
331+
:meth:`~pymongo.database.Database.drop_collection`, however,
332+
as indexes will not be removed.
335333
336334
If `safe` is ``True`` returns the response to the *lastError*
337335
command. Otherwise, returns ``None``.
338336
339337
:Parameters:
340-
- `spec_or_object_id` (optional): a ``dict`` or
341-
:class:`~pymongo.son.SON` instance specifying which documents
342-
should be removed; or an instance of
343-
:class:`~pymongo.objectid.ObjectId` specifying the value of the
344-
``_id`` field for the document to be removed
338+
- `spec_or_id` (optional): a dictionary specifying the
339+
documents to be removed OR any other type specifying the
340+
value of ``"_id"`` for the document to be removed
345341
- `safe` (optional): check that the remove succeeded?
346342
343+
.. versionchanged:: 1.6+
344+
Accept any type other than a ``dict`` instance for removal
345+
by ``"_id"``, not just :class:`~pymongo.objectid.ObjectId`
346+
instances.
347347
.. versionchanged:: 1.4
348348
Return the response to *lastError* if `safe` is ``True``.
349349
.. versionchanged:: 1.2
350-
The `spec_or_object_id` parameter is now optional. If it is
350+
The `spec_or_id` parameter is now optional. If it is
351351
not specified *all* documents in the collection will be
352352
removed.
353353
.. versionadded:: 1.1
354354
The `safe` parameter.
355355
356356
.. mongodoc:: remove
357357
"""
358-
#TODO accept anything but dict as an _id
359-
spec = spec_or_object_id
360-
if spec is None:
361-
spec = {}
362-
if isinstance(spec, ObjectId):
363-
spec = {"_id": spec}
364-
365-
if not isinstance(spec, dict):
366-
raise TypeError("spec must be an instance of dict, not %s" %
367-
type(spec))
358+
if spec_or_id is None:
359+
spec_or_id = {}
360+
if not isinstance(spec_or_id, dict):
361+
spec_or_id = {"_id": spec_or_id}
368362

369363
return self.__database.connection._send_message(
370-
message.delete(self.__full_name, spec, safe), safe)
364+
message.delete(self.__full_name, spec_or_id, safe), safe)
371365

372366
def find_one(self, spec_or_id=None, *args, **kwargs):
373367
"""Get a single document from the database.

test/test_collection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,16 @@ def test_find_one_non_objectid(self):
856856
self.assert_(db.test.find_one(5))
857857
self.failIf(db.test.find_one(6))
858858

859+
def test_remove_non_objectid(self):
860+
db = self.db
861+
db.drop_collection("test")
862+
863+
db.test.save({"_id": 5})
864+
865+
self.assertEqual(1, db.test.count())
866+
db.test.remove(5)
867+
self.assertEqual(0, db.test.count())
868+
859869
def test_find_one_with_find_args(self):
860870
db = self.db
861871
db.drop_collection("test")

test/test_database.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,6 @@ def test_remove(self):
364364
db = self.connection.pymongo_test
365365
db.test.remove({})
366366

367-
self.assertRaises(TypeError, db.test.remove, 5)
368-
self.assertRaises(TypeError, db.test.remove, "test")
369-
self.assertRaises(TypeError, db.test.remove, [])
370-
371367
one = db.test.save({"x": 1})
372368
db.test.save({"x": 2})
373369
db.test.save({"x": 3})

0 commit comments

Comments
 (0)