Skip to content

Commit 8b95755

Browse files
committed
DOCSP-47282: RawBSONDocument information
1 parent fe2c41a commit 8b95755

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

source/data-formats/bson.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,32 @@ The following example reads the sample BSON document from ``file.bson``:
117117

118118
{"address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"}
119119

120+
RawBSONDocument
121+
---------------
122+
123+
You might prefer to use raw BSON documents in certain cases, such as when you move a
124+
document between databases or collections, when you must write binary blobs to a
125+
disk, or when you want to bypass the performance overhead of converting to and from
126+
{+language+} dictionaries. You can use the ``RawBSONDocument`` class to represent raw BSON
127+
documents. To use ``RawBSONDocument`` objects with your collection, set the
128+
``document_class`` parameter of the ``MongoClient`` constructor to ``RawBSONDocument``.
129+
130+
The following example fetches the sample BSON document from a collection and converts it
131+
to a ``RawBSONDocument``:
132+
133+
.. code-block:: python
134+
135+
from bson.raw_bson import RawBSONDocument
136+
137+
client = pymongo.MongoClient("<connection URI>", document_class=RawBSONDocument)
138+
collection = client["<database name>"]["<collection name>"]
139+
raw_doc = collection.find_one({"name": "Mongo's Pizza"})
140+
141+
.. note::
142+
143+
``RawBSONDocument`` objects are read-only. To modify a ``RawBSONDocument``, you must
144+
first convert it to a {+language+} dictionary.
145+
120146
API Documentation
121147
-----------------
122148

0 commit comments

Comments
 (0)