Represents a delete_one operation.
| 194 | |
| 195 | |
| 196 | class DeleteOne(_DeleteOp): |
| 197 | """Represents a delete_one operation.""" |
| 198 | |
| 199 | __slots__ = () |
| 200 | |
| 201 | def __init__( |
| 202 | self, |
| 203 | filter: Mapping[str, Any], |
| 204 | collation: Optional[_CollationIn] = None, |
| 205 | hint: Optional[_IndexKeyHint] = None, |
| 206 | namespace: Optional[str] = None, |
| 207 | ) -> None: |
| 208 | """Create a DeleteOne instance. |
| 209 | |
| 210 | For use with :meth:`~pymongo.asynchronous.collection.AsyncCollection.bulk_write`, :meth:`~pymongo.collection.Collection.bulk_write`, |
| 211 | :meth:`~pymongo.asynchronous.mongo_client.AsyncMongoClient.bulk_write` and :meth:`~pymongo.mongo_client.MongoClient.bulk_write`. |
| 212 | |
| 213 | :param filter: A query that matches the document to delete. |
| 214 | :param collation: An instance of |
| 215 | :class:`~pymongo.collation.Collation`. |
| 216 | :param hint: An index to use to support the query |
| 217 | predicate specified either by its string name, or in the same |
| 218 | format as passed to |
| 219 | :meth:`~pymongo.asynchronous.collection.AsyncCollection.create_index` or :meth:`~pymongo.collection.Collection.create_index` (e.g. |
| 220 | ``[('field', ASCENDING)]``). This option is only supported on |
| 221 | MongoDB 4.4 and above. |
| 222 | :param namespace: (optional) The namespace in which to delete a document. |
| 223 | |
| 224 | .. versionchanged:: 4.9 |
| 225 | Added the `namespace` option to support `MongoClient.bulk_write`. |
| 226 | .. versionchanged:: 3.11 |
| 227 | Added the ``hint`` option. |
| 228 | .. versionchanged:: 3.5 |
| 229 | Added the `collation` option. |
| 230 | """ |
| 231 | super().__init__(filter, collation, hint, namespace) |
| 232 | |
| 233 | def _add_to_bulk(self, bulkobj: _AgnosticBulk) -> None: |
| 234 | """Add this operation to the _AsyncBulk/_Bulk instance `bulkobj`.""" |
| 235 | bulkobj.add_delete( |
| 236 | self._filter, |
| 237 | 1, |
| 238 | collation=validate_collation_or_none(self._collation), |
| 239 | hint=self._hint, |
| 240 | ) |
| 241 | |
| 242 | def _add_to_client_bulk(self, bulkobj: _AgnosticClientBulk) -> None: |
| 243 | """Add this operation to the _AsyncClientBulk/_ClientBulk instance `bulkobj`.""" |
| 244 | if not self._namespace: |
| 245 | raise InvalidOperation( |
| 246 | "MongoClient.bulk_write requires a namespace to be provided for each write operation" |
| 247 | ) |
| 248 | bulkobj.add_delete( |
| 249 | self._namespace, |
| 250 | self._filter, |
| 251 | multi=False, |
| 252 | collation=validate_collation_or_none(self._collation), |
| 253 | hint=self._hint, |
no outgoing calls