Skip to content

Commit 9dab0b8

Browse files
committed
DOCSP-48166: Add async examples for Data Formats pages
1 parent 4188490 commit 9dab0b8

File tree

4 files changed

+558
-196
lines changed

4 files changed

+558
-196
lines changed

source/data-formats/bson.txt

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,20 @@ To read BSON documents from a file, open a file stream in read-binary mode on th
9999
file. Then, decode the documents from BSON format as you read them by using the ``bson.decode()``
100100
method.
101101

102-
The following example reads the sample BSON document from ``file.bson``:
102+
The following example reads the sample BSON document from ``file.bson``. Select the
103+
:guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
104+
code.
103105

104106
.. io-code-block::
105107
:copyable: true
106108

107109
.. input::
108110
:language: python
109111

110-
with open("file.bson", "rb") as file:
111-
data = file.read()
112-
document = bson.decode(data)
113-
print(document)
112+
with open("file.bson", "rb") as file:
113+
data = file.read()
114+
document = bson.decode(data)
115+
print(document)
114116

115117
.. output::
116118
:visible: false
@@ -140,23 +142,49 @@ constructor to ``RawBSONDocument``.
140142
The following example configures a ``MongoClient`` object to use ``RawBSONDocument`` objects
141143
to model the collection, then retrieves the sample document from the preceding examples:
142144

143-
.. io-code-block::
144-
:copyable: true
145+
.. tabs::
146+
147+
.. tab:: Synchronous
148+
:tabid: sync
145149

146-
.. input::
147-
:language: python
148-
149-
from bson.raw_bson import RawBSONDocument
150+
.. io-code-block::
151+
:copyable: true
150152

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))
153+
.. input::
154+
:language: python
155+
156+
from bson.raw_bson import RawBSONDocument
155157

156-
.. output::
157-
:visible: false
158+
client = pymongo.MongoClient("<connection URI>", document_class=RawBSONDocument)
159+
collection = client.sample_restaurants.restaurants
160+
raw_doc = collection.find_one({"name": "Mongo's Pizza"})
161+
print(type(raw_doc))
162+
163+
.. output::
164+
:visible: false
165+
166+
<class 'bson.raw_bson.RawBSONDocument'>
167+
168+
.. tab:: Asynchronous
169+
:tabid: async
170+
171+
.. io-code-block::
172+
:copyable: true
173+
174+
.. input::
175+
:language: python
176+
177+
from bson.raw_bson import RawBSONDocument
178+
179+
client = pymongo.AsyncMongoClient("<connection URI>", document_class=RawBSONDocument)
180+
collection = client.sample_restaurants.restaurants
181+
raw_doc = await collection.find_one({"name": "Mongo's Pizza"})
182+
print(type(raw_doc))
183+
184+
.. output::
185+
:visible: false
158186

159-
<class 'bson.raw_bson.RawBSONDocument'>
187+
<class 'bson.raw_bson.RawBSONDocument'>
160188

161189
API Documentation
162190
-----------------

source/data-formats/custom-types.txt

Lines changed: 98 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -306,24 +306,50 @@ The following code example shows this process:
306306
collection = db.get_collection("test", codec_options=codec_options)
307307

308308
You can then use this reference to a collection to store instances of the ``Decimal``
309-
class:
309+
class. Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the
310+
corresponding code.
310311

311-
.. io-code-block::
312-
:copyable: true
312+
.. tabs::
313+
314+
.. tab:: Synchronous
315+
:tabid: sync
313316

314-
.. input::
315-
:language: python
317+
.. io-code-block::
318+
:copyable: true
316319

317-
import pprint
320+
.. input::
321+
:language: python
318322

319-
collection.insert_one({"num": Decimal("45.321")})
320-
my_doc = collection.find_one()
321-
pprint.pprint(my_doc)
323+
import pprint
322324

323-
.. output::
324-
:language: shell
325+
collection.insert_one({"num": Decimal("45.321")})
326+
my_doc = collection.find_one()
327+
pprint.pprint(my_doc)
325328

326-
{'_id': ObjectId('...'), 'num': Decimal128('45.321')}
329+
.. output::
330+
:language: shell
331+
332+
{'_id': ObjectId('...'), 'num': Decimal128('45.321')}
333+
334+
.. tab:: Asynchronous
335+
:tabid: async
336+
337+
.. io-code-block::
338+
:copyable: true
339+
340+
.. input::
341+
:language: python
342+
343+
import pprint
344+
345+
await collection.insert_one({"num": Decimal("45.321")})
346+
my_doc = await collection.find_one()
347+
pprint.pprint(my_doc)
348+
349+
.. output::
350+
:language: shell
351+
352+
{'_id': ObjectId('...'), 'num': Decimal128('45.321')}
327353

328354
.. note::
329355

@@ -383,36 +409,74 @@ back into Python objects:
383409
return value
384410

385411
You can then add the ``PickledBinaryDecoder`` to the codec options for a collection
386-
and use it to encode and decode your custom types:
412+
and use it to encode and decode your custom types. Select the :guilabel:`Synchronous` or
413+
:guilabel:`Asynchronous` tab to see the corresponding code.
387414

388-
.. io-code-block::
389-
:copyable: true
415+
.. tabs::
416+
417+
.. tab:: Synchronous
418+
:tabid: sync
390419

391-
.. input::
392-
:language: python
420+
.. io-code-block::
421+
:copyable: true
393422

394-
from bson.codec_options import CodecOptions,TypeRegistry
423+
.. input::
424+
:language: python
395425

396-
codec_options = CodecOptions(
397-
type_registry=TypeRegistry(
398-
[PickledBinaryDecoder()], fallback_encoder=fallback_pickle_encoder
399-
)
400-
)
426+
from bson.codec_options import CodecOptions,TypeRegistry
401427

402-
collection = db.get_collection("test", codec_options=codec_options)
403-
collection.insert_one(
404-
{"_id": 1, "str": MyStringType("hello world"), "num": MyNumberType(2)}
405-
)
406-
my_doc = collection.find_one()
428+
codec_options = CodecOptions(
429+
type_registry=TypeRegistry(
430+
[PickledBinaryDecoder()], fallback_encoder=fallback_pickle_encoder
431+
)
432+
)
407433

408-
print(isinstance(my_doc["str"], MyStringType))
409-
print(isinstance(my_doc["num"], MyNumberType))
434+
collection = db.get_collection("test", codec_options=codec_options)
435+
collection.insert_one(
436+
{"_id": 1, "str": MyStringType("hello world"), "num": MyNumberType(2)}
437+
)
438+
my_doc = collection.find_one()
410439

411-
.. output::
412-
:language: shell
440+
print(isinstance(my_doc["str"], MyStringType))
441+
print(isinstance(my_doc["num"], MyNumberType))
442+
443+
.. output::
444+
:language: shell
445+
446+
True
447+
True
448+
449+
.. tab:: Asynchronous
450+
:tabid: async
451+
452+
.. io-code-block::
453+
:copyable: true
454+
455+
.. input::
456+
:language: python
457+
458+
from bson.codec_options import CodecOptions,TypeRegistry
459+
460+
codec_options = CodecOptions(
461+
type_registry=TypeRegistry(
462+
[PickledBinaryDecoder()], fallback_encoder=fallback_pickle_encoder
463+
)
464+
)
465+
466+
collection = db.get_collection("test", codec_options=codec_options)
467+
await collection.insert_one(
468+
{"_id": 1, "str": MyStringType("hello world"), "num": MyNumberType(2)}
469+
)
470+
my_doc = await collection.find_one()
471+
472+
print(isinstance(my_doc["str"], MyStringType))
473+
print(isinstance(my_doc["num"], MyNumberType))
474+
475+
.. output::
476+
:language: shell
413477

414-
True
415-
True
478+
True
479+
True
416480

417481
Limitations
418482
-----------

0 commit comments

Comments
 (0)