MCPcopy Create free account
hub / github.com/mongodb/mongo-python-driver / dereference

Method dereference

pymongo/synchronous/database.py:1439–1479  ·  view source on GitHub ↗

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[ClientSession] = None,
        comment: Optional[Any] = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

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

Callers 4

test_derefMethod · 0.45
test_deref_kwargsMethod · 0.45
test_derefMethod · 0.45
test_deref_kwargsMethod · 0.45

Calls 1

find_oneMethod · 0.45

Tested by 4

test_derefMethod · 0.36
test_deref_kwargsMethod · 0.36
test_derefMethod · 0.36
test_deref_kwargsMethod · 0.36