Get a single document from the database. All arguments to :meth:`find` are also valid arguments for :meth:`find_one`, although any `limit` argument will be ignored. Returns a single document, or ``None`` if no matching document is found. The :meth:`find_one`
(
self, filter: Optional[Any] = None, *args: Any, **kwargs: Any
)
| 1721 | ) |
| 1722 | |
| 1723 | def find_one( |
| 1724 | self, filter: Optional[Any] = None, *args: Any, **kwargs: Any |
| 1725 | ) -> Optional[_DocumentType]: |
| 1726 | """Get a single document from the database. |
| 1727 | |
| 1728 | All arguments to :meth:`find` are also valid arguments for |
| 1729 | :meth:`find_one`, although any `limit` argument will be |
| 1730 | ignored. Returns a single document, or ``None`` if no matching |
| 1731 | document is found. |
| 1732 | |
| 1733 | The :meth:`find_one` method obeys the :attr:`read_preference` of |
| 1734 | this :class:`Collection`. |
| 1735 | |
| 1736 | :param filter: a dictionary specifying |
| 1737 | the query to be performed OR any other type to be used as |
| 1738 | the value for a query for ``"_id"``. |
| 1739 | |
| 1740 | :param args: any additional positional arguments |
| 1741 | are the same as the arguments to :meth:`find`. |
| 1742 | |
| 1743 | :param kwargs: any additional keyword arguments |
| 1744 | are the same as the arguments to :meth:`find`. |
| 1745 | |
| 1746 | :: code-block: python |
| 1747 | |
| 1748 | >>> collection.find_one(max_time_ms=100) |
| 1749 | |
| 1750 | """ |
| 1751 | if filter is not None and not isinstance(filter, abc.Mapping): |
| 1752 | filter = {"_id": filter} |
| 1753 | cursor = self.find(filter, *args, **kwargs) |
| 1754 | for result in cursor.limit(-1): |
| 1755 | return result |
| 1756 | return None |
| 1757 | |
| 1758 | def find(self, *args: Any, **kwargs: Any) -> Cursor[_DocumentType]: |
| 1759 | """Query the database. |
no test coverage detected