Skip to content

Commit 30c4da2

Browse files
committed
DOCSP-47282: RawBSONDocument information (#176)
(cherry picked from commit 052b97b)
1 parent c272148 commit 30c4da2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

source/data-formats/bson.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,47 @@ 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+
Work with Raw BSON Data
121+
-----------------------
122+
123+
{+driver-short+} supports the usage of raw BSON documents. The following list contains
124+
some situations that might require using raw BSON documents:
125+
126+
- Moving a document between databases or collections
127+
- Writing binary data to a disk
128+
- Bypassing the performance overhead of converting to and from {+language+} dictionaries
129+
130+
The ``RawBSONDocument`` class is a representation of a BSON document that provides
131+
access to the underlying raw BSON bytes. To use ``RawBSONDocument`` objects to represent
132+
documents in your collection, set the ``document_class`` parameter of the ``MongoClient``
133+
constructor to ``RawBSONDocument``.
134+
135+
.. note::
136+
137+
``RawBSONDocument`` objects are read-only. To modify a ``RawBSONDocument``, you must
138+
first convert it to a {+language+} dictionary.
139+
140+
The following example configures a ``MongoClient`` object to use ``RawBSONDocument`` objects
141+
to model the collection, then retrieves the sample document from the preceding examples:
142+
143+
.. io-code-block::
144+
:copyable: true
145+
146+
.. input::
147+
:language: python
148+
149+
from bson.raw_bson import RawBSONDocument
150+
151+
client = pymongo.MongoClient("<connection URI>", document_class=RawBSONDocument)
152+
collection = client.sample_restaurants.restaurants
153+
raw_doc = collection.find_one({"name": "Mongo's Pizza"})
154+
print(type(raw_doc))
155+
156+
.. output::
157+
:visible: false
158+
159+
<class 'bson.raw_bson.RawBSONDocument'>
160+
120161
API Documentation
121162
-----------------
122163

0 commit comments

Comments
 (0)