Skip to content

Commit 8b6e930

Browse files
committed
Merge pull request mongodb#164 from shelman/master
Added full_response parameter to find_and_modify for PYTHON-492
2 parents 517356c + acb4829 commit 8b6e930

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ The following is a list of people who have contributed to
6161
- Michael Henson (hensom)
6262
- Craig Hobbs (craigahobbs)
6363
- Emily Stolfo (estolfo)
64+
- Sam Helman (shelman)

pymongo/collection.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ def inline_map_reduce(self, map, reduce, full_response=False, **kwargs):
13021302
return res.get("results")
13031303

13041304
def find_and_modify(self, query={}, update=None,
1305-
upsert=False, sort=None, **kwargs):
1305+
upsert=False, sort=None, full_response=False, **kwargs):
13061306
"""Update and return an object.
13071307
13081308
This is a thin wrapper around the findAndModify_ command. The
@@ -1315,13 +1315,21 @@ def find_and_modify(self, query={}, update=None,
13151315
parameter. If no objects match the `query` and `upsert` is false,
13161316
returns ``None``. If upserting and `new` is false, returns ``{}``.
13171317
1318+
If the full_response parameter is true, the return value will be
1319+
the entire response object from the server, including the 'ok' and
1320+
'lastErrorObject' fields, rather than just the modified object.
1321+
This is useful mainly because the 'lastErrorObject' document holds
1322+
information about the command's execution.
1323+
13181324
:Parameters:
13191325
- `query`: filter for the update (default ``{}``)
13201326
- `update`: see second argument to :meth:`update` (no default)
13211327
- `upsert`: insert if object doesn't exist (default ``False``)
13221328
- `sort`: a list of (key, direction) pairs specifying the sort
13231329
order for this query. See :meth:`~pymongo.cursor.Cursor.sort`
13241330
for details.
1331+
- `full_response`: return the entire response object from the
1332+
server
13251333
- `remove`: remove rather than updating (default ``False``)
13261334
- `new`: return updated rather than original object
13271335
(default ``False``)
@@ -1336,6 +1344,9 @@ def find_and_modify(self, query={}, update=None,
13361344
13371345
.. note:: Requires server version **>= 1.3.0**
13381346
1347+
.. versionchanged:: 2.5
1348+
Added the optional full_response parameter
1349+
13391350
.. versionchanged:: 2.4
13401351
Deprecated the use of mapping types for the sort parameter
13411352
@@ -1385,7 +1396,10 @@ def find_and_modify(self, query={}, update=None,
13851396
# Should never get here b/c of allowable_errors
13861397
raise ValueError("Unexpected Error: %s" % (out,))
13871398

1388-
return out.get('value')
1399+
if full_response:
1400+
return out
1401+
else:
1402+
return out.get('value')
13891403

13901404
def __iter__(self):
13911405
return self

test/test_collection.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,21 @@ def test_find_and_modify(self):
17321732
c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
17331733
new=True, fields={'i': 1}))
17341734

1735+
# Test with full_response=True (version > 2.4.2)
1736+
result = c.find_and_modify({'_id': 1}, {'$inc': {'i': 1}},
1737+
new=True, upsert=True,
1738+
full_response=True,
1739+
fields={'i': 1})
1740+
self.assertEqual({'_id': 1, 'i': 5}, result["value"])
1741+
self.assertEqual(True, result["lastErrorObject"]["updatedExisting"])
1742+
1743+
result = c.find_and_modify({'_id': 2}, {'$inc': {'i': 1}},
1744+
new=True, upsert=True,
1745+
full_response=True,
1746+
fields={'i': 1})
1747+
self.assertEqual({'_id': 2, 'i': 1}, result["value"])
1748+
self.assertEqual(False, result["lastErrorObject"]["updatedExisting"])
1749+
17351750
class ExtendedDict(dict):
17361751
pass
17371752

0 commit comments

Comments
 (0)