Skip to content

Commit 1608aca

Browse files
committed
PYTHON-4925 Stop sending multi:False and upsert:False
1 parent 239ba22 commit 1608aca

File tree

4 files changed

+46
-66
lines changed

4 files changed

+46
-66
lines changed

pymongo/asynchronous/bulk.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,22 @@ def add_update(
140140
self,
141141
selector: Mapping[str, Any],
142142
update: Union[Mapping[str, Any], _Pipeline],
143-
multi: bool = False,
144-
upsert: bool = False,
143+
multi: bool,
144+
upsert: Optional[bool],
145145
collation: Optional[Mapping[str, Any]] = None,
146146
array_filters: Optional[list[Mapping[str, Any]]] = None,
147147
hint: Union[str, dict[str, Any], None] = None,
148148
sort: Optional[Mapping[str, Any]] = None,
149149
) -> None:
150150
"""Create an update document and add it to the list of ops."""
151151
validate_ok_for_update(update)
152-
cmd: dict[str, Any] = dict( # noqa: C406
153-
[("q", selector), ("u", update), ("multi", multi), ("upsert", upsert)]
154-
)
152+
cmd: dict[str, Any] = {"q": selector, "u": update}
153+
if multi:
154+
cmd["multi"] = multi
155+
# A bulk_write containing an update_many is not retryable.
156+
self.is_retryable = False
157+
if upsert is not None:
158+
cmd["upsert"] = upsert
155159
if collation is not None:
156160
self.uses_collation = True
157161
cmd["collation"] = collation
@@ -164,23 +168,22 @@ def add_update(
164168
if sort is not None:
165169
self.uses_sort = True
166170
cmd["sort"] = sort
167-
if multi:
168-
# A bulk_write containing an update_many is not retryable.
169-
self.is_retryable = False
170171
self.ops.append((_UPDATE, cmd))
171172

172173
def add_replace(
173174
self,
174175
selector: Mapping[str, Any],
175176
replacement: Mapping[str, Any],
176-
upsert: bool = False,
177+
upsert: Optional[bool],
177178
collation: Optional[Mapping[str, Any]] = None,
178179
hint: Union[str, dict[str, Any], None] = None,
179180
sort: Optional[Mapping[str, Any]] = None,
180181
) -> None:
181182
"""Create a replace document and add it to the list of ops."""
182183
validate_ok_for_replace(replacement)
183-
cmd = {"q": selector, "u": replacement, "multi": False, "upsert": upsert}
184+
cmd = {"q": selector, "u": replacement}
185+
if upsert is not None:
186+
cmd["upsert"] = upsert
184187
if collation is not None:
185188
self.uses_collation = True
186189
cmd["collation"] = collation

pymongo/asynchronous/client_bulk.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,13 @@ def __init__(
106106
self.bypass_doc_val = bypass_document_validation
107107
self.comment = comment
108108
self.verbose_results = verbose_results
109-
110109
self.ops: list[tuple[str, Mapping[str, Any]]] = []
111110
self.namespaces: list[str] = []
112111
self.idx_offset: int = 0
113112
self.total_ops: int = 0
114-
115113
self.executed = False
116-
self.uses_upsert = False
117114
self.uses_collation = False
118115
self.uses_array_filters = False
119-
self.uses_hint_update = False
120-
self.uses_hint_delete = False
121-
self.uses_sort = False
122-
123116
self.is_retryable = self.client.options.retry_writes
124117
self.retrying = False
125118
self.started_retryable_write = False
@@ -144,7 +137,7 @@ def add_update(
144137
namespace: str,
145138
selector: Mapping[str, Any],
146139
update: Union[Mapping[str, Any], _Pipeline],
147-
multi: bool = False,
140+
multi: bool,
148141
upsert: Optional[bool] = None,
149142
collation: Optional[Mapping[str, Any]] = None,
150143
array_filters: Optional[list[Mapping[str, Any]]] = None,
@@ -157,26 +150,23 @@ def add_update(
157150
"update": -1,
158151
"filter": selector,
159152
"updateMods": update,
160-
"multi": multi,
161153
}
154+
if multi:
155+
# A bulk_write containing an update_many is not retryable.
156+
self.is_retryable = False
157+
cmd["multi"] = multi
162158
if upsert is not None:
163-
self.uses_upsert = True
164159
cmd["upsert"] = upsert
165160
if array_filters is not None:
166161
self.uses_array_filters = True
167162
cmd["arrayFilters"] = array_filters
168163
if hint is not None:
169-
self.uses_hint_update = True
170164
cmd["hint"] = hint
171165
if collation is not None:
172166
self.uses_collation = True
173167
cmd["collation"] = collation
174168
if sort is not None:
175-
self.uses_sort = True
176169
cmd["sort"] = sort
177-
if multi:
178-
# A bulk_write containing an update_many is not retryable.
179-
self.is_retryable = False
180170
self.ops.append(("update", cmd))
181171
self.namespaces.append(namespace)
182172
self.total_ops += 1
@@ -200,16 +190,13 @@ def add_replace(
200190
"multi": False,
201191
}
202192
if upsert is not None:
203-
self.uses_upsert = True
204193
cmd["upsert"] = upsert
205194
if hint is not None:
206-
self.uses_hint_update = True
207195
cmd["hint"] = hint
208196
if collation is not None:
209197
self.uses_collation = True
210198
cmd["collation"] = collation
211199
if sort is not None:
212-
self.uses_sort = True
213200
cmd["sort"] = sort
214201
self.ops.append(("replace", cmd))
215202
self.namespaces.append(namespace)
@@ -224,16 +211,16 @@ def add_delete(
224211
hint: Union[str, dict[str, Any], None] = None,
225212
) -> None:
226213
"""Create a delete document and add it to the list of ops."""
227-
cmd = {"delete": -1, "filter": selector, "multi": multi}
214+
cmd = {"delete": -1, "filter": selector}
215+
if multi:
216+
cmd["multi"] = multi
217+
# A bulk_write containing an update_many is not retryable.
218+
self.is_retryable = False
228219
if hint is not None:
229-
self.uses_hint_delete = True
230220
cmd["hint"] = hint
231221
if collation is not None:
232222
self.uses_collation = True
233223
cmd["collation"] = collation
234-
if multi:
235-
# A bulk_write containing an update_many is not retryable.
236-
self.is_retryable = False
237224
self.ops.append(("delete", cmd))
238225
self.namespaces.append(namespace)
239226
self.total_ops += 1

pymongo/synchronous/bulk.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,22 @@ def add_update(
140140
self,
141141
selector: Mapping[str, Any],
142142
update: Union[Mapping[str, Any], _Pipeline],
143-
multi: bool = False,
144-
upsert: bool = False,
143+
multi: bool,
144+
upsert: Optional[bool],
145145
collation: Optional[Mapping[str, Any]] = None,
146146
array_filters: Optional[list[Mapping[str, Any]]] = None,
147147
hint: Union[str, dict[str, Any], None] = None,
148148
sort: Optional[Mapping[str, Any]] = None,
149149
) -> None:
150150
"""Create an update document and add it to the list of ops."""
151151
validate_ok_for_update(update)
152-
cmd: dict[str, Any] = dict( # noqa: C406
153-
[("q", selector), ("u", update), ("multi", multi), ("upsert", upsert)]
154-
)
152+
cmd: dict[str, Any] = {"q": selector, "u": update}
153+
if multi:
154+
cmd["multi"] = multi
155+
# A bulk_write containing an update_many is not retryable.
156+
self.is_retryable = False
157+
if upsert is not None:
158+
cmd["upsert"] = upsert
155159
if collation is not None:
156160
self.uses_collation = True
157161
cmd["collation"] = collation
@@ -164,23 +168,22 @@ def add_update(
164168
if sort is not None:
165169
self.uses_sort = True
166170
cmd["sort"] = sort
167-
if multi:
168-
# A bulk_write containing an update_many is not retryable.
169-
self.is_retryable = False
170171
self.ops.append((_UPDATE, cmd))
171172

172173
def add_replace(
173174
self,
174175
selector: Mapping[str, Any],
175176
replacement: Mapping[str, Any],
176-
upsert: bool = False,
177+
upsert: Optional[bool],
177178
collation: Optional[Mapping[str, Any]] = None,
178179
hint: Union[str, dict[str, Any], None] = None,
179180
sort: Optional[Mapping[str, Any]] = None,
180181
) -> None:
181182
"""Create a replace document and add it to the list of ops."""
182183
validate_ok_for_replace(replacement)
183-
cmd = {"q": selector, "u": replacement, "multi": False, "upsert": upsert}
184+
cmd = {"q": selector, "u": replacement}
185+
if upsert is not None:
186+
cmd["upsert"] = upsert
184187
if collation is not None:
185188
self.uses_collation = True
186189
cmd["collation"] = collation

pymongo/synchronous/client_bulk.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,13 @@ def __init__(
106106
self.bypass_doc_val = bypass_document_validation
107107
self.comment = comment
108108
self.verbose_results = verbose_results
109-
110109
self.ops: list[tuple[str, Mapping[str, Any]]] = []
111110
self.namespaces: list[str] = []
112111
self.idx_offset: int = 0
113112
self.total_ops: int = 0
114-
115113
self.executed = False
116-
self.uses_upsert = False
117114
self.uses_collation = False
118115
self.uses_array_filters = False
119-
self.uses_hint_update = False
120-
self.uses_hint_delete = False
121-
self.uses_sort = False
122-
123116
self.is_retryable = self.client.options.retry_writes
124117
self.retrying = False
125118
self.started_retryable_write = False
@@ -144,7 +137,7 @@ def add_update(
144137
namespace: str,
145138
selector: Mapping[str, Any],
146139
update: Union[Mapping[str, Any], _Pipeline],
147-
multi: bool = False,
140+
multi: bool,
148141
upsert: Optional[bool] = None,
149142
collation: Optional[Mapping[str, Any]] = None,
150143
array_filters: Optional[list[Mapping[str, Any]]] = None,
@@ -157,26 +150,23 @@ def add_update(
157150
"update": -1,
158151
"filter": selector,
159152
"updateMods": update,
160-
"multi": multi,
161153
}
154+
if multi:
155+
# A bulk_write containing an update_many is not retryable.
156+
self.is_retryable = False
157+
cmd["multi"] = multi
162158
if upsert is not None:
163-
self.uses_upsert = True
164159
cmd["upsert"] = upsert
165160
if array_filters is not None:
166161
self.uses_array_filters = True
167162
cmd["arrayFilters"] = array_filters
168163
if hint is not None:
169-
self.uses_hint_update = True
170164
cmd["hint"] = hint
171165
if collation is not None:
172166
self.uses_collation = True
173167
cmd["collation"] = collation
174168
if sort is not None:
175-
self.uses_sort = True
176169
cmd["sort"] = sort
177-
if multi:
178-
# A bulk_write containing an update_many is not retryable.
179-
self.is_retryable = False
180170
self.ops.append(("update", cmd))
181171
self.namespaces.append(namespace)
182172
self.total_ops += 1
@@ -200,16 +190,13 @@ def add_replace(
200190
"multi": False,
201191
}
202192
if upsert is not None:
203-
self.uses_upsert = True
204193
cmd["upsert"] = upsert
205194
if hint is not None:
206-
self.uses_hint_update = True
207195
cmd["hint"] = hint
208196
if collation is not None:
209197
self.uses_collation = True
210198
cmd["collation"] = collation
211199
if sort is not None:
212-
self.uses_sort = True
213200
cmd["sort"] = sort
214201
self.ops.append(("replace", cmd))
215202
self.namespaces.append(namespace)
@@ -224,16 +211,16 @@ def add_delete(
224211
hint: Union[str, dict[str, Any], None] = None,
225212
) -> None:
226213
"""Create a delete document and add it to the list of ops."""
227-
cmd = {"delete": -1, "filter": selector, "multi": multi}
214+
cmd = {"delete": -1, "filter": selector}
215+
if multi:
216+
cmd["multi"] = multi
217+
# A bulk_write containing an update_many is not retryable.
218+
self.is_retryable = False
228219
if hint is not None:
229-
self.uses_hint_delete = True
230220
cmd["hint"] = hint
231221
if collation is not None:
232222
self.uses_collation = True
233223
cmd["collation"] = collation
234-
if multi:
235-
# A bulk_write containing an update_many is not retryable.
236-
self.is_retryable = False
237224
self.ops.append(("delete", cmd))
238225
self.namespaces.append(namespace)
239226
self.total_ops += 1

0 commit comments

Comments
 (0)