Dereference a :class:`~bson.dbref.DBRef`, getting the document it points to. Raises :class:`TypeError` if `dbref` is not an instance of :class:`~bson.dbref.DBRef`. Returns a document, or ``None`` if the reference does not point to a valid document. Raises :c
(
self,
dbref: DBRef,
session: Optional[AsyncClientSession] = None,
comment: Optional[Any] = None,
**kwargs: Any,
)
| 1440 | return result |
| 1441 | |
| 1442 | async def dereference( |
| 1443 | self, |
| 1444 | dbref: DBRef, |
| 1445 | session: Optional[AsyncClientSession] = None, |
| 1446 | comment: Optional[Any] = None, |
| 1447 | **kwargs: Any, |
| 1448 | ) -> Optional[_DocumentType]: |
| 1449 | """Dereference a :class:`~bson.dbref.DBRef`, getting the |
| 1450 | document it points to. |
| 1451 | |
| 1452 | Raises :class:`TypeError` if `dbref` is not an instance of |
| 1453 | :class:`~bson.dbref.DBRef`. Returns a document, or ``None`` if |
| 1454 | the reference does not point to a valid document. Raises |
| 1455 | :class:`ValueError` if `dbref` has a database specified that |
| 1456 | is different from the current database. |
| 1457 | |
| 1458 | :param dbref: the reference |
| 1459 | :param session: a |
| 1460 | :class:`~pymongo.asynchronous.client_session.AsyncClientSession`. |
| 1461 | :param comment: A user-provided comment to attach to this |
| 1462 | command. |
| 1463 | :param kwargs: any additional keyword arguments |
| 1464 | are the same as the arguments to |
| 1465 | :meth:`~pymongo.asynchronous.collection.AsyncCollection.find`. |
| 1466 | |
| 1467 | |
| 1468 | .. versionchanged:: 4.1 |
| 1469 | Added ``comment`` parameter. |
| 1470 | .. versionchanged:: 3.6 |
| 1471 | Added ``session`` parameter. |
| 1472 | """ |
| 1473 | if not isinstance(dbref, DBRef): |
| 1474 | raise TypeError("cannot dereference a %s" % type(dbref)) |
| 1475 | if dbref.database is not None and dbref.database != self._name: |
| 1476 | raise ValueError( |
| 1477 | "trying to dereference a DBRef that points to " |
| 1478 | f"another database ({dbref.database!r} not {self._name!r})" |
| 1479 | ) |
| 1480 | return await self[dbref.collection].find_one( |
| 1481 | {"_id": dbref.id}, session=session, comment=comment, **kwargs |
| 1482 | ) |