A reference to a document stored in MongoDB.
| 23 | |
| 24 | |
| 25 | class DBRef: |
| 26 | """A reference to a document stored in MongoDB.""" |
| 27 | |
| 28 | __slots__ = "__collection", "__id", "__database", "__kwargs" |
| 29 | __getstate__ = _getstate_slots |
| 30 | __setstate__ = _setstate_slots |
| 31 | # DBRef isn't actually a BSON "type" so this number was arbitrarily chosen. |
| 32 | _type_marker = 100 |
| 33 | |
| 34 | def __init__( |
| 35 | self, |
| 36 | collection: str, |
| 37 | id: Any, |
| 38 | database: Optional[str] = None, |
| 39 | _extra: Optional[Mapping[str, Any]] = None, |
| 40 | **kwargs: Any, |
| 41 | ) -> None: |
| 42 | """Initialize a new :class:`DBRef`. |
| 43 | |
| 44 | Raises :class:`TypeError` if `collection` or `database` is not |
| 45 | an instance of :class:`str`. `database` is optional and allows |
| 46 | references to documents to work across databases. Any additional |
| 47 | keyword arguments will create additional fields in the resultant |
| 48 | embedded document. |
| 49 | |
| 50 | :param collection: name of the collection the document is stored in |
| 51 | :param id: the value of the document's ``"_id"`` field |
| 52 | :param database: name of the database to reference |
| 53 | :param kwargs: additional keyword arguments will |
| 54 | create additional, custom fields |
| 55 | |
| 56 | .. seealso:: The MongoDB documentation on `dbrefs <https://dochub.mongodb.org/core/dbrefs>`_. |
| 57 | """ |
| 58 | if not isinstance(collection, str): |
| 59 | raise TypeError(f"collection must be an instance of str, not {type(collection)}") |
| 60 | if database is not None and not isinstance(database, str): |
| 61 | raise TypeError(f"database must be an instance of str, not {type(database)}") |
| 62 | |
| 63 | self.__collection = collection |
| 64 | self.__id = id |
| 65 | self.__database = database |
| 66 | kwargs.update(_extra or {}) |
| 67 | self.__kwargs = kwargs |
| 68 | |
| 69 | @property |
| 70 | def collection(self) -> str: |
| 71 | """Get the name of this DBRef's collection.""" |
| 72 | return self.__collection |
| 73 | |
| 74 | @property |
| 75 | def id(self) -> Any: |
| 76 | """Get this DBRef's _id.""" |
| 77 | return self.__id |
| 78 | |
| 79 | @property |
| 80 | def database(self) -> Optional[str]: |
| 81 | """Get the name of this DBRef's database. |
| 82 |
no outgoing calls