Skip to content

Commit 1323ef1

Browse files
committed
PYTHON-2036 Expand CRUD API support for index hinting (also PYTHON-2015, PYTHON-2104, PYTHON-2134)
1 parent a460725 commit 1323ef1

10 files changed

+1300
-76
lines changed

pymongo/bulk.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def __init__(self, collection, ordered, bypass_document_validation):
156156
self.bypass_doc_val = bypass_document_validation
157157
self.uses_collation = False
158158
self.uses_array_filters = False
159+
self.uses_hint = False
159160
self.is_retryable = True
160161
self.retrying = False
161162
self.started_retryable_write = False
@@ -180,7 +181,7 @@ def add_insert(self, document):
180181
self.ops.append((_INSERT, document))
181182

182183
def add_update(self, selector, update, multi=False, upsert=False,
183-
collation=None, array_filters=None):
184+
collation=None, array_filters=None, hint=None):
184185
"""Create an update document and add it to the list of ops.
185186
"""
186187
validate_ok_for_update(update)
@@ -193,13 +194,16 @@ def add_update(self, selector, update, multi=False, upsert=False,
193194
if array_filters is not None:
194195
self.uses_array_filters = True
195196
cmd['arrayFilters'] = array_filters
197+
if hint is not None:
198+
self.uses_hint = True
199+
cmd['hint'] = hint
196200
if multi:
197201
# A bulk_write containing an update_many is not retryable.
198202
self.is_retryable = False
199203
self.ops.append((_UPDATE, cmd))
200204

201205
def add_replace(self, selector, replacement, upsert=False,
202-
collation=None):
206+
collation=None, hint=None):
203207
"""Create a replace document and add it to the list of ops.
204208
"""
205209
validate_ok_for_replace(replacement)
@@ -209,6 +213,9 @@ def add_replace(self, selector, replacement, upsert=False,
209213
if collation is not None:
210214
self.uses_collation = True
211215
cmd['collation'] = collation
216+
if hint is not None:
217+
self.uses_hint = True
218+
cmd['hint'] = hint
212219
self.ops.append((_UPDATE, cmd))
213220

214221
def add_delete(self, selector, limit, collation=None):
@@ -252,9 +259,13 @@ def gen_unordered(self):
252259

253260
def _execute_command(self, generator, write_concern, session,
254261
sock_info, op_id, retryable, full_result):
255-
if sock_info.max_wire_version < 5 and self.uses_collation:
256-
raise ConfigurationError(
257-
'Must be connected to MongoDB 3.4+ to use a collation.')
262+
if sock_info.max_wire_version < 5:
263+
if self.uses_collation:
264+
raise ConfigurationError(
265+
'Must be connected to MongoDB 3.4+ to use a collation.')
266+
if self.uses_hint:
267+
raise ConfigurationError(
268+
'Must be connected to MongoDB 3.4+ to use hint.')
258269
if sock_info.max_wire_version < 6 and self.uses_array_filters:
259270
raise ConfigurationError(
260271
'Must be connected to MongoDB 3.6+ to use arrayFilters.')
@@ -428,6 +439,9 @@ def execute_no_results(self, sock_info, generator):
428439
if self.uses_array_filters:
429440
raise ConfigurationError(
430441
'arrayFilters is unsupported for unacknowledged writes.')
442+
if self.uses_hint:
443+
raise ConfigurationError(
444+
'hint is unsupported for unacknowledged writes.')
431445
# Cannot have both unacknowledged writes and bypass document validation.
432446
if self.bypass_doc_val and sock_info.max_wire_version >= 4:
433447
raise OperationFailure("Cannot set bypass_document_validation with"

0 commit comments

Comments
 (0)