@@ -1125,7 +1125,7 @@ def drop(self, session=None):
1125
1125
def _delete (
1126
1126
self , sock_info , criteria , multi ,
1127
1127
write_concern = None , op_id = None , ordered = True ,
1128
- collation = None , session = None , retryable_write = False ):
1128
+ collation = None , hint = None , session = None , retryable_write = False ):
1129
1129
"""Internal delete helper."""
1130
1130
common .validate_is_mapping ("filter" , criteria )
1131
1131
write_concern = write_concern or self .write_concern
@@ -1142,6 +1142,16 @@ def _delete(
1142
1142
'Collation is unsupported for unacknowledged writes.' )
1143
1143
else :
1144
1144
delete_doc ['collation' ] = collation
1145
+ if hint is not None :
1146
+ if sock_info .max_wire_version < 5 :
1147
+ raise ConfigurationError (
1148
+ 'Must be connected to MongoDB 3.4+ to use hint.' )
1149
+ elif not acknowledged :
1150
+ raise ConfigurationError (
1151
+ 'hint is unsupported for unacknowledged writes.' )
1152
+ if not isinstance (hint , string_type ):
1153
+ hint = helpers ._index_document (hint )
1154
+ delete_doc ['hint' ] = hint
1145
1155
command = SON ([('delete' , self .name ),
1146
1156
('ordered' , ordered ),
1147
1157
('deletes' , [delete_doc ])])
@@ -1171,20 +1181,20 @@ def _delete(
1171
1181
def _delete_retryable (
1172
1182
self , criteria , multi ,
1173
1183
write_concern = None , op_id = None , ordered = True ,
1174
- collation = None , session = None ):
1184
+ collation = None , hint = None , session = None ):
1175
1185
"""Internal delete helper."""
1176
1186
def _delete (session , sock_info , retryable_write ):
1177
1187
return self ._delete (
1178
1188
sock_info , criteria , multi ,
1179
1189
write_concern = write_concern , op_id = op_id , ordered = ordered ,
1180
- collation = collation , session = session ,
1190
+ collation = collation , hint = hint , session = session ,
1181
1191
retryable_write = retryable_write )
1182
1192
1183
1193
return self .__database .client ._retryable_write (
1184
1194
(write_concern or self .write_concern ).acknowledged and not multi ,
1185
1195
_delete , session )
1186
1196
1187
- def delete_one (self , filter , collation = None , session = None ):
1197
+ def delete_one (self , filter , collation = None , hint = None , session = None ):
1188
1198
"""Delete a single document matching the filter.
1189
1199
1190
1200
>>> db.test.count_documents({'x': 1})
@@ -1200,29 +1210,35 @@ def delete_one(self, filter, collation=None, session=None):
1200
1210
- `collation` (optional): An instance of
1201
1211
:class:`~pymongo.collation.Collation`. This option is only supported
1202
1212
on MongoDB 3.4 and above.
1213
+ - `hint` (optional): An index to use to support the query
1214
+ predicate specified either by its string name, or in the same
1215
+ format as passed to
1216
+ :meth:`~pymongo.collection.Collection.create_index` (e.g.
1217
+ ``[('field', ASCENDING)]``). This option is only supported on
1218
+ MongoDB 4.4 and above.
1203
1219
- `session` (optional): a
1204
1220
:class:`~pymongo.client_session.ClientSession`.
1205
1221
1206
1222
:Returns:
1207
1223
- An instance of :class:`~pymongo.results.DeleteResult`.
1208
1224
1225
+ .. versionchanged:: 3.11
1226
+ Added ``hint`` parameter.
1209
1227
.. versionchanged:: 3.6
1210
1228
Added ``session`` parameter.
1211
-
1212
1229
.. versionchanged:: 3.4
1213
1230
Added the `collation` option.
1214
-
1215
1231
.. versionadded:: 3.0
1216
1232
"""
1217
1233
write_concern = self ._write_concern_for (session )
1218
1234
return DeleteResult (
1219
1235
self ._delete_retryable (
1220
1236
filter , False ,
1221
1237
write_concern = write_concern ,
1222
- collation = collation , session = session ),
1238
+ collation = collation , hint = hint , session = session ),
1223
1239
write_concern .acknowledged )
1224
1240
1225
- def delete_many (self , filter , collation = None , session = None ):
1241
+ def delete_many (self , filter , collation = None , hint = None , session = None ):
1226
1242
"""Delete one or more documents matching the filter.
1227
1243
1228
1244
>>> db.test.count_documents({'x': 1})
@@ -1238,26 +1254,32 @@ def delete_many(self, filter, collation=None, session=None):
1238
1254
- `collation` (optional): An instance of
1239
1255
:class:`~pymongo.collation.Collation`. This option is only supported
1240
1256
on MongoDB 3.4 and above.
1257
+ - `hint` (optional): An index to use to support the query
1258
+ predicate specified either by its string name, or in the same
1259
+ format as passed to
1260
+ :meth:`~pymongo.collection.Collection.create_index` (e.g.
1261
+ ``[('field', ASCENDING)]``). This option is only supported on
1262
+ MongoDB 4.4 and above.
1241
1263
- `session` (optional): a
1242
1264
:class:`~pymongo.client_session.ClientSession`.
1243
1265
1244
1266
:Returns:
1245
1267
- An instance of :class:`~pymongo.results.DeleteResult`.
1246
1268
1269
+ .. versionchanged:: 3.11
1270
+ Added ``hint`` parameter.
1247
1271
.. versionchanged:: 3.6
1248
1272
Added ``session`` parameter.
1249
-
1250
1273
.. versionchanged:: 3.4
1251
1274
Added the `collation` option.
1252
-
1253
1275
.. versionadded:: 3.0
1254
1276
"""
1255
1277
write_concern = self ._write_concern_for (session )
1256
1278
return DeleteResult (
1257
1279
self ._delete_retryable (
1258
1280
filter , True ,
1259
1281
write_concern = write_concern ,
1260
- collation = collation , session = session ),
1282
+ collation = collation , hint = hint , session = session ),
1261
1283
write_concern .acknowledged )
1262
1284
1263
1285
def find_one (self , filter = None , * args , ** kwargs ):
@@ -2849,10 +2871,8 @@ def inline_map_reduce(self, map, reduce, full_response=False, session=None,
2849
2871
2850
2872
.. versionchanged:: 3.6
2851
2873
Added ``session`` parameter.
2852
-
2853
2874
.. versionchanged:: 3.4
2854
2875
Added the `collation` option.
2855
-
2856
2876
"""
2857
2877
res = self ._map_reduce (map , reduce , {"inline" : 1 }, session ,
2858
2878
self .read_preference , ** kwargs )
@@ -2931,7 +2951,8 @@ def _find_and_modify(session, sock_info, retryable_write):
2931
2951
write_concern .acknowledged , _find_and_modify , session )
2932
2952
2933
2953
def find_one_and_delete (self , filter ,
2934
- projection = None , sort = None , session = None , ** kwargs ):
2954
+ projection = None , sort = None , hint = None ,
2955
+ session = None , ** kwargs ):
2935
2956
"""Finds a single document and deletes it, returning the document.
2936
2957
2937
2958
>>> db.test.count_documents({'x': 1})
@@ -2968,15 +2989,21 @@ def find_one_and_delete(self, filter,
2968
2989
- `sort` (optional): a list of (key, direction) pairs
2969
2990
specifying the sort order for the query. If multiple documents
2970
2991
match the query, they are sorted and the first is deleted.
2992
+ - `hint` (optional): An index to use to support the query predicate
2993
+ specified either by its string name, or in the same format as
2994
+ passed to :meth:`~pymongo.collection.Collection.create_index`
2995
+ (e.g. ``[('field', ASCENDING)]``). This option is only supported
2996
+ on MongoDB 4.4 and above.
2971
2997
- `session` (optional): a
2972
2998
:class:`~pymongo.client_session.ClientSession`.
2973
2999
- `**kwargs` (optional): additional command arguments can be passed
2974
3000
as keyword arguments (for example maxTimeMS can be used with
2975
3001
recent server versions).
2976
3002
3003
+ .. versionchanged:: 3.11
3004
+ Added ``hint`` parameter.
2977
3005
.. versionchanged:: 3.6
2978
3006
Added ``session`` parameter.
2979
-
2980
3007
.. versionchanged:: 3.2
2981
3008
Respects write concern.
2982
3009
@@ -2989,11 +3016,10 @@ def find_one_and_delete(self, filter,
2989
3016
.. versionchanged:: 3.4
2990
3017
Added the `collation` option.
2991
3018
.. versionadded:: 3.0
2992
-
2993
3019
"""
2994
3020
kwargs ['remove' ] = True
2995
3021
return self .__find_and_modify (filter , projection , sort ,
2996
- session = session , ** kwargs )
3022
+ hint = hint , session = session , ** kwargs )
2997
3023
2998
3024
def find_one_and_replace (self , filter , replacement ,
2999
3025
projection = None , sort = None , upsert = False ,
0 commit comments