Skip to content

Commit 2716395

Browse files
author
Mike Dirolf
committed
return the entire response to lastError for update/remove if safe is True PYTHON-87
1 parent 3b733b9 commit 2716395

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

pymongo/collection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ def update(self, spec, document,
239239
>>> list(db.test.find())
240240
[{u'a': u'c', u'x': u'y', u'_id': ObjectId('...')}]
241241
242-
If `safe` is ``True`` returns the number of documents affected
243-
by the :meth:`update`. Otherwise, returns ``None``.
242+
If `safe` is ``True`` returns the response to the *lastError*
243+
command. Otherwise, returns ``None``.
244244
245245
:Parameters:
246246
- `spec`: a ``dict`` or :class:`~pymongo.son.SON` instance specifying
@@ -259,7 +259,7 @@ def update(self, spec, document,
259259
for that change.
260260
261261
.. versionchanged:: 1.3+
262-
Return the number of updated documents if `safe` is ``True``.
262+
Return the response to *lastError* if `safe` is ``True``.
263263
.. versionadded:: 1.1.1
264264
The `multi` parameter.
265265
@@ -298,8 +298,8 @@ def remove(self, spec_or_object_id=None, safe=False):
298298
:meth:`~pymongo.database.Database.drop_collection`, however, as
299299
indexes will not be removed.
300300
301-
If `safe` is ``True`` returns the number of documents affected
302-
by the :meth:`remove`. Otherwise, returns ``None``.
301+
If `safe` is ``True`` returns the response to the *lastError*
302+
command. Otherwise, returns ``None``.
303303
304304
:Parameters:
305305
- `spec_or_object_id` (optional): a ``dict`` or
@@ -310,7 +310,7 @@ def remove(self, spec_or_object_id=None, safe=False):
310310
- `safe` (optional): check that the remove succeeded?
311311
312312
.. versionchanged:: 1.3+
313-
Return the number of removed documents if `safe` is ``True``.
313+
Return the response to *lastError* if `safe` is ``True``.
314314
.. versionchanged:: 1.2
315315
The `spec_or_object_id` parameter is now optional. If it is
316316
not specified *all* documents in the collection will be

pymongo/connection.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ def __check_response_to_last_error(self, response):
430430
`response` is a byte string representing a response to the message.
431431
If it represents an error response we raise OperationFailure.
432432
433-
Return the value of ``'n'`` from the response (the number of
434-
documents affected by the command).
433+
Return the response as a document.
435434
"""
436435
response = helpers._unpack_response(response)
437436

@@ -440,7 +439,7 @@ def __check_response_to_last_error(self, response):
440439

441440
# TODO unify logic with database.error method
442441
if error.get("err", 0) is None:
443-
return error["n"]
442+
return error
444443
if error["err"] == "not master":
445444
self._reset()
446445

@@ -449,15 +448,15 @@ def __check_response_to_last_error(self, response):
449448
else:
450449
raise OperationFailure(error["err"])
451450

452-
return error["n"]
451+
return error
453452

454453
def _send_message(self, message, with_last_error=False):
455454
"""Say something to Mongo.
456455
457456
Raises ConnectionFailure if the message cannot be sent. Raises
458457
OperationFailure if `with_last_error` is ``True`` and the
459458
response to the getLastError call returns an error. Return the
460-
number of affected documents, or ``None`` if `with_last_error`
459+
response from lastError, or ``None`` if `with_last_error`
461460
is ``False``.
462461
463462
:Parameters:

test/test_collection.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def test_multi_update(self):
546546
self.assertEqual(5, doc["y"])
547547

548548
self.assertEqual(2, db.test.update({"x": 4}, {"$set": {"y": 6}},
549-
multi=True, safe=True))
549+
multi=True, safe=True)["n"])
550550

551551
def test_upsert(self):
552552
db = self.db
@@ -577,8 +577,9 @@ def test_safe_update(self):
577577
self.assertRaises(OperationFailure, db.test.update,
578578
{"_id": id}, {"$inc": {"x": 1}}, safe=True)
579579

580-
self.assertEqual(1, db.test.update({"_id": id}, {"$inc": {"x": 2}}, safe=True))
581-
self.assertEqual(0, db.test.update({"_id": "foo"}, {"$inc": {"x": 2}}, safe=True))
580+
self.assertEqual(1, db.test.update({"_id": id}, {"$inc": {"x": 2}}, safe=True)["n"])
581+
582+
self.assertEqual(0, db.test.update({"_id": "foo"}, {"$inc": {"x": 2}}, safe=True)["n"])
582583

583584
def test_safe_save(self):
584585
db = self.db
@@ -610,8 +611,8 @@ def test_safe_remove(self):
610611
db.drop_collection("test")
611612
db.test.insert({"x": 1})
612613
db.test.insert({"x": 1})
613-
self.assertEqual(2, db.test.remove({}, safe=True))
614-
self.assertEqual(0, db.test.remove({}, safe=True))
614+
self.assertEqual(2, db.test.remove({}, safe=True)["n"])
615+
self.assertEqual(0, db.test.remove({}, safe=True)["n"])
615616

616617
def test_count(self):
617618
db = self.db

0 commit comments

Comments
 (0)