Skip to content

Commit f5b44ea

Browse files
aherlihybehackett
authored andcommitted
PYTHON-982 - Support bypassDocumentValidation
1 parent bcf0d57 commit f5b44ea

File tree

4 files changed

+359
-40
lines changed

4 files changed

+359
-40
lines changed

pymongo/bulk.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def _merge_command(run, full_result, results):
199199
class _Bulk(object):
200200
"""The private guts of the bulk write API.
201201
"""
202-
def __init__(self, collection, ordered):
202+
def __init__(self, collection, ordered, bypass_document_validation):
203203
"""Initialize a _Bulk instance.
204204
"""
205205
self.collection = collection
@@ -208,6 +208,7 @@ def __init__(self, collection, ordered):
208208
self.name = "%s.%s" % (collection.database.name, collection.name)
209209
self.namespace = collection.database.name + '.$cmd'
210210
self.executed = False
211+
self.bypass_doc_val = bypass_document_validation
211212

212213
def add_insert(self, document):
213214
"""Add an insert document to the list of ops.
@@ -289,6 +290,8 @@ def execute_command(self, sock_info, generator, write_concern):
289290
('ordered', self.ordered)])
290291
if write_concern.document:
291292
cmd['writeConcern'] = write_concern.document
293+
if self.bypass_doc_val and sock_info.max_wire_version >= 4:
294+
cmd['bypassDocumentValidation'] = True
292295

293296
bwc = _BulkWriteContext(db_name, cmd, sock_info, op_id, listeners)
294297
results = _do_batched_write_command(
@@ -320,26 +323,30 @@ def execute_no_results(self, sock_info, generator):
320323
for run in generator:
321324
try:
322325
if run.op_type == _INSERT:
323-
coll._insert(sock_info,
324-
run.ops,
325-
self.ordered,
326-
write_concern=write_concern,
327-
op_id=op_id)
326+
coll._insert(
327+
sock_info,
328+
run.ops,
329+
self.ordered,
330+
write_concern=write_concern,
331+
op_id=op_id,
332+
bypass_doc_val=self.bypass_doc_val)
328333
elif run.op_type == _UPDATE:
329334
for operation in run.ops:
330335
doc = operation['u']
331336
check_keys = True
332337
if doc and next(iter(doc)).startswith('$'):
333338
check_keys = False
334-
coll._update(sock_info,
335-
operation['q'],
336-
doc,
337-
operation['upsert'],
338-
check_keys,
339-
operation['multi'],
340-
write_concern=write_concern,
341-
op_id=op_id,
342-
ordered=self.ordered)
339+
coll._update(
340+
sock_info,
341+
operation['q'],
342+
doc,
343+
operation['upsert'],
344+
check_keys,
345+
operation['multi'],
346+
write_concern=write_concern,
347+
op_id=op_id,
348+
ordered=self.ordered,
349+
bypass_doc_val=self.bypass_doc_val)
343350
else:
344351
for operation in run.ops:
345352
coll._delete(sock_info,
@@ -556,7 +563,8 @@ class BulkOperationBuilder(object):
556563

557564
__slots__ = '__bulk'
558565

559-
def __init__(self, collection, ordered=True):
566+
def __init__(self, collection, ordered=True,
567+
bypass_document_validation=False):
560568
"""Initialize a new BulkOperationBuilder instance.
561569
562570
:Parameters:
@@ -567,8 +575,17 @@ def __init__(self, collection, ordered=True):
567575
in arbitrary order (possibly in parallel on the server), reporting
568576
any errors that occurred after attempting all operations. Defaults
569577
to ``True``.
578+
- `bypass_document_validation`: (optional) If ``True``, allows the
579+
write to opt-out of document level validation. Default is
580+
``False``.
581+
582+
.. note:: `bypass_document_validation` requires server version
583+
**>= 3.2**
584+
585+
.. versionchanged:: 3.2
586+
Added bypass_document_validation support
570587
"""
571-
self.__bulk = _Bulk(collection, ordered)
588+
self.__bulk = _Bulk(collection, ordered, bypass_document_validation)
572589

573590
def find(self, selector):
574591
"""Specify selection criteria for bulk operations.

0 commit comments

Comments
 (0)