minor: cleanup, tests and doc for last commit

This commit is contained in:
Mike Dirolf 2009-11-30 10:28:45 -05:00
parent 2896911928
commit 7fb44ebb6b
4 changed files with 42 additions and 10 deletions

View File

@ -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)

View File

@ -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)")

View File

@ -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" %

View File

@ -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({})