Skip to content

Commit 016fc4f

Browse files
committed
Better nModified handling PYTHON-647
In a sharded cluster with mixed version shards (i.e. one shard primary is 2.6, another is 2.4 or older) the result of one call to update could include nModified when the next does not. Since there is no way to provide a valid count in this case we omit the field. Make sure you upgrade your entire cluster to MongoDB 2.6 or newer before relying on this field.
1 parent 2538f2c commit 016fc4f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

doc/examples/bulk.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ describing the type and count of operations performed.
8484
>>>
8585

8686
.. warning:: ``nModified`` is only reported by MongoDB 2.6 and later. When
87-
connected to an earlier server version, PyMongo omits this field from the
88-
results of a bulk write operation.
87+
connected to an earlier server version, or in certain mixed version sharding
88+
configurations, PyMongo omits this field from the results of a bulk
89+
write operation.
8990

9091
The first write failure that occurs (e.g. duplicate key error) aborts the
9192
remaining operations, and PyMongo raises

pymongo/bulk.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,15 @@ def _merge_command(run, full_result, results):
152152
full_result["nMatched"] += (affected - n_upserted)
153153
else:
154154
full_result["nMatched"] += affected
155-
full_result["nModified"] += result.get("nModified", 0)
155+
n_modified = result.get("nModified")
156+
# SERVER-13001 - in a mixed sharded cluster a call to
157+
# update could return nModified (>= 2.6) or not (<= 2.4).
158+
# If any call does not return nModified we can't report
159+
# a valid final count so omit the field completely.
160+
if n_modified is not None and "nModified" in full_result:
161+
full_result["nModified"] += n_modified
162+
else:
163+
full_result.pop("nModified", None)
156164

157165
write_errors = result.get("writeErrors")
158166
if write_errors:

0 commit comments

Comments
 (0)