Delete a single document matching the filter. >>> db.test.count_documents({'x': 1}) 3 >>> result = db.test.delete_one({'x': 1}) >>> result.deleted_count 1 >>> db.test.count_documents({'x': 1}) 2 :param filter: A query th
(
self,
filter: Mapping[str, Any],
collation: Optional[_CollationIn] = None,
hint: Optional[_IndexKeyHint] = None,
session: Optional[ClientSession] = None,
let: Optional[Mapping[str, Any]] = None,
comment: Optional[Any] = None,
)
| 1591 | ) |
| 1592 | |
| 1593 | def delete_one( |
| 1594 | self, |
| 1595 | filter: Mapping[str, Any], |
| 1596 | collation: Optional[_CollationIn] = None, |
| 1597 | hint: Optional[_IndexKeyHint] = None, |
| 1598 | session: Optional[ClientSession] = None, |
| 1599 | let: Optional[Mapping[str, Any]] = None, |
| 1600 | comment: Optional[Any] = None, |
| 1601 | ) -> DeleteResult: |
| 1602 | """Delete a single document matching the filter. |
| 1603 | |
| 1604 | >>> db.test.count_documents({'x': 1}) |
| 1605 | 3 |
| 1606 | >>> result = db.test.delete_one({'x': 1}) |
| 1607 | >>> result.deleted_count |
| 1608 | 1 |
| 1609 | >>> db.test.count_documents({'x': 1}) |
| 1610 | 2 |
| 1611 | |
| 1612 | :param filter: A query that matches the document to delete. |
| 1613 | :param collation: An instance of |
| 1614 | :class:`~pymongo.collation.Collation`. |
| 1615 | :param hint: An index to use to support the query |
| 1616 | predicate specified either by its string name, or in the same |
| 1617 | format as passed to |
| 1618 | :meth:`~pymongo.collection.Collection.create_index` (e.g. |
| 1619 | ``[('field', ASCENDING)]``). This option is only supported on |
| 1620 | MongoDB 4.4 and above. |
| 1621 | :param session: a |
| 1622 | :class:`~pymongo.client_session.ClientSession`. |
| 1623 | :param let: Map of parameter names and values. Values must be |
| 1624 | constant or closed expressions that do not reference document |
| 1625 | fields. Parameters can then be accessed as variables in an |
| 1626 | aggregate expression context (e.g. "$$var"). |
| 1627 | :param comment: A user-provided comment to attach to this |
| 1628 | command. |
| 1629 | |
| 1630 | :return: - An instance of :class:`~pymongo.results.DeleteResult`. |
| 1631 | |
| 1632 | .. versionchanged:: 4.1 |
| 1633 | Added ``let`` parameter. |
| 1634 | Added ``comment`` parameter. |
| 1635 | .. versionchanged:: 3.11 |
| 1636 | Added ``hint`` parameter. |
| 1637 | .. versionchanged:: 3.6 |
| 1638 | Added ``session`` parameter. |
| 1639 | .. versionchanged:: 3.4 |
| 1640 | Added the `collation` option. |
| 1641 | .. versionadded:: 3.0 |
| 1642 | """ |
| 1643 | write_concern = self._write_concern_for(session) |
| 1644 | return DeleteResult( |
| 1645 | self._delete_retryable( |
| 1646 | filter, |
| 1647 | False, |
| 1648 | write_concern=write_concern, |
| 1649 | collation=collation, |
| 1650 | hint=hint, |
no test coverage detected