diff --git a/doc/contributors.rst b/doc/contributors.rst index ba77d28ef..49ab1155f 100644 --- a/doc/contributors.rst +++ b/doc/contributors.rst @@ -14,3 +14,4 @@ The following is a list of people who have contributed to - Fajran Iman Rusadi (fajran) - Brad Clements (bkc) - Andrey Fedorov (andreyf) +- Joshua Roesslein (joshthecoder) diff --git a/gridfs/__init__.py b/gridfs/__init__.py index ecff4c84f..182a6d80b 100644 --- a/gridfs/__init__.py +++ b/gridfs/__init__.py @@ -74,6 +74,9 @@ class GridFS(object): spec = filename_or_spec if isinstance(filename_or_spec, types.StringTypes): spec = {"filename": filename_or_spec} + if not isinstance(spec, dict): + raise TypeError("filename_or_spec must be an " + "instance of (str, dict, SON)") if not isinstance(collection, types.StringTypes): raise TypeError("collection must be an instance of (str, unicode)") diff --git a/pymongo/collection.py b/pymongo/collection.py index a6200ef24..13fbd5688 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -239,25 +239,42 @@ class Collection(object): spec, document, safe), safe) def remove(self, spec_or_object_id=None, safe=False): - """Remove an object(s) from this collection. + """Remove a document(s) from this collection. - Raises TypeEror if the argument is not an instance of - (dict, ObjectId). If `safe` is True then the remove will be checked for - errors, raising OperationFailure if one occurred. Safe removes wait for - a response from the database, while normal removes do not. + .. warning:: Calls to :meth:`remove` should be performed with + care, as removed data cannot be restored. + Raises :class:`~pymongo.errors.TypeError` if + `spec_or_object_id` is not an instance of (``dict``, + :class:`~pymongo.objectid.ObjectId`). If `safe` is ``True`` + then the remove operation will be checked for errors, raising + :class:`~pymongo.errors.OperationFailure` if one + occurred. Safe removes wait for a response from the database, + while normal removes do not. + + If no `spec_or_object_id` is given all documents in this + collection will be removed. This is not equivalent to calling + :meth:`~pymongo.database.Database.drop_collection`, however, as + indexes will not be removed. :Parameters: - - `spec_or_object_id` (optional): a SON object specifying elements - which must be present for a document to be removed OR an instance - of ObjectId to be used as the value for an _id element + - `spec_or_object_id` (optional): a ``dict`` or + :class:`~pymongo.son.SON` instance specifying which documents + should be removed; or an instance of + :class:`~pymongo.objectid.ObjectId` specifying the value of the + ``_id`` field for the document to be removed - `safe` (optional): check that the remove succeeded? + + .. versionchanged:: 1.1.2+ + The `spec_or_object_id` parameter is now optional. If it is + not specified *all* documents in the collection will be + removed. """ spec = spec_or_object_id if spec is None: - spec = SON() + spec = {} if isinstance(spec, ObjectId): - spec = SON({"_id": spec}) + spec = {"_id": spec} if not isinstance(spec, types.DictType): raise TypeError("spec must be an instance of dict, not %s" % diff --git a/test/test_collection.py b/test/test_collection.py index fbbd4a684..697314941 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -300,6 +300,17 @@ class TestCollection(unittest.TestCase): qcheck.check_unittest(self, remove_insert_find_one, qcheck.gen_mongo_dict(3)) + def test_remove_all(self): + self.db.test.remove() + self.assertEqual(0, self.db.test.count()) + + self.db.test.insert({"x": 1}) + self.db.test.insert({"y": 1}) + self.assertEqual(2, self.db.test.count()) + + self.db.test.remove() + self.assertEqual(0, self.db.test.count()) + def test_find_w_fields(self): db = self.db db.test.remove({})