Check if a file exists in this instance of :class:`GridFS`. The file to check for can be specified by the value of its ``_id`` key, or by passing in a query document. A query document can be passed in as dictionary, or by using keyword arguments. Thus, the following
(
self,
document_or_id: Optional[Any] = None,
session: Optional[ClientSession] = None,
**kwargs: Any,
)
| 406 | return GridOutCursor(self._collection, *args, **kwargs) |
| 407 | |
| 408 | def exists( |
| 409 | self, |
| 410 | document_or_id: Optional[Any] = None, |
| 411 | session: Optional[ClientSession] = None, |
| 412 | **kwargs: Any, |
| 413 | ) -> bool: |
| 414 | """Check if a file exists in this instance of :class:`GridFS`. |
| 415 | |
| 416 | The file to check for can be specified by the value of its |
| 417 | ``_id`` key, or by passing in a query document. A query |
| 418 | document can be passed in as dictionary, or by using keyword |
| 419 | arguments. Thus, the following three calls are equivalent: |
| 420 | |
| 421 | >>> fs.exists(file_id) |
| 422 | >>> fs.exists({"_id": file_id}) |
| 423 | >>> fs.exists(_id=file_id) |
| 424 | |
| 425 | As are the following two calls: |
| 426 | |
| 427 | >>> fs.exists({"filename": "mike.txt"}) |
| 428 | >>> fs.exists(filename="mike.txt") |
| 429 | |
| 430 | And the following two: |
| 431 | |
| 432 | >>> fs.exists({"foo": {"$gt": 12}}) |
| 433 | >>> fs.exists(foo={"$gt": 12}) |
| 434 | |
| 435 | Returns ``True`` if a matching file exists, ``False`` |
| 436 | otherwise. Calls to :meth:`exists` will not automatically |
| 437 | create appropriate indexes; application developers should be |
| 438 | sure to create indexes if needed and as appropriate. |
| 439 | |
| 440 | :param document_or_id: query document, or _id of the |
| 441 | document to check for |
| 442 | :param session: a |
| 443 | :class:`~pymongo.client_session.ClientSession` |
| 444 | :param kwargs: keyword arguments are used as a |
| 445 | query document, if they're present. |
| 446 | |
| 447 | .. versionchanged:: 3.6 |
| 448 | Added ``session`` parameter. |
| 449 | """ |
| 450 | _disallow_transactions(session) |
| 451 | if kwargs: |
| 452 | f = self._files.find_one(kwargs, ["_id"], session=session) |
| 453 | else: |
| 454 | f = self._files.find_one(document_or_id, ["_id"], session=session) |
| 455 | |
| 456 | return f is not None |
| 457 | |
| 458 | |
| 459 | class GridFSBucket: |