MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / RawBSONDocument

Class RawBSONDocument

bson/raw_bson.py:77–179  ·  view source on GitHub ↗

Representation for a MongoDB document that provides access to the raw BSON bytes that compose it. Only when a field is accessed or modified within the document does RawBSONDocument decode its bytes.

Source from the content-addressed store, hash-verified

75
76
77class RawBSONDocument(Mapping[str, Any]):
78 """Representation for a MongoDB document that provides access to the raw
79 BSON bytes that compose it.
80
81 Only when a field is accessed or modified within the document does
82 RawBSONDocument decode its bytes.
83 """
84
85 __slots__ = ("__raw", "__inflated_doc", "__codec_options")
86 _type_marker = _RAW_BSON_DOCUMENT_MARKER
87 __codec_options: CodecOptions[RawBSONDocument]
88
89 def __init__(
90 self,
91 bson_bytes: bytes | memoryview,
92 codec_options: Optional[CodecOptions[RawBSONDocument]] = None,
93 ) -> None:
94 """Create a new :class:`RawBSONDocument`
95
96 :class:`RawBSONDocument` is a representation of a BSON document that
97 provides access to the underlying raw BSON bytes. Only when a field is
98 accessed or modified within the document does RawBSONDocument decode
99 its bytes.
100
101 :class:`RawBSONDocument` implements the ``Mapping`` abstract base
102 class from the standard library so it can be used like a read-only
103 ``dict``::
104
105 >>> from bson import encode
106 >>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'}))
107 >>> raw_doc.raw
108 b'...'
109 >>> raw_doc['_id']
110 'my_doc'
111
112 :param bson_bytes: the BSON bytes that compose this document
113 :param codec_options: An instance of
114 :class:`~bson.codec_options.CodecOptions` whose ``document_class``
115 must be :class:`RawBSONDocument`. The default is
116 :attr:`DEFAULT_RAW_BSON_OPTIONS`.
117
118 .. versionchanged:: 3.8
119 :class:`RawBSONDocument` now validates that the ``bson_bytes``
120 passed in represent a single bson document.
121
122 .. versionchanged:: 3.5
123 If a :class:`~bson.codec_options.CodecOptions` is passed in, its
124 `document_class` must be :class:`RawBSONDocument`.
125 """
126 self.__raw = bson_bytes
127 self.__inflated_doc: Optional[Mapping[str, Any]] = None
128 # Can't default codec_options to DEFAULT_RAW_BSON_OPTIONS in signature,
129 # it refers to this class RawBSONDocument.
130 if codec_options is None:
131 codec_options = DEFAULT_RAW_BSON_OPTIONS
132 elif not issubclass(codec_options.document_class, RawBSONDocument):
133 raise TypeError(
134 "RawBSONDocument cannot use CodecOptions with document "

Calls

no outgoing calls

Tested by 15

test_empty_docMethod · 0.72
test_invalid_bson_eooMethod · 0.72
test_contains_dbrefMethod · 0.72
test_rawMethod · 0.72
test_insert_oneMethod · 0.72