Skip to content

DOCSP-48166: Add async examples for Data Formats pages #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions source/data-formats/bson.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ The following example reads the sample BSON document from ``file.bson``:
.. input::
:language: python

with open("file.bson", "rb") as file:
data = file.read()
document = bson.decode(data)
print(document)
with open("file.bson", "rb") as file:
data = file.read()
document = bson.decode(data)
print(document)

.. output::
:visible: false
Expand Down Expand Up @@ -138,25 +138,53 @@ constructor to ``RawBSONDocument``.
first convert it to a {+language+} dictionary.

The following example configures a ``MongoClient`` object to use ``RawBSONDocument`` objects
to model the collection, then retrieves the sample document from the preceding examples:
to model the collection, then retrieves the sample document from the preceding examples.
Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding
code.

.. io-code-block::
:copyable: true
.. tabs::

.. tab:: Synchronous
:tabid: sync

.. input::
:language: python

from bson.raw_bson import RawBSONDocument
.. io-code-block::
:copyable: true

client = pymongo.MongoClient("<connection URI>", document_class=RawBSONDocument)
collection = client.sample_restaurants.restaurants
raw_doc = collection.find_one({"name": "Mongo's Pizza"})
print(type(raw_doc))
.. input::
:language: python
from bson.raw_bson import RawBSONDocument

.. output::
:visible: false
client = pymongo.MongoClient("<connection URI>", document_class=RawBSONDocument)
collection = client.sample_restaurants.restaurants
raw_doc = collection.find_one({"name": "Mongo's Pizza"})
print(type(raw_doc))

.. output::
:visible: false

<class 'bson.raw_bson.RawBSONDocument'>

.. tab:: Asynchronous
:tabid: async

.. io-code-block::
:copyable: true

.. input::
:language: python

from bson.raw_bson import RawBSONDocument

client = pymongo.AsyncMongoClient("<connection URI>", document_class=RawBSONDocument)
collection = client.sample_restaurants.restaurants
raw_doc = await collection.find_one({"name": "Mongo's Pizza"})
print(type(raw_doc))

.. output::
:visible: false

<class 'bson.raw_bson.RawBSONDocument'>
<class 'bson.raw_bson.RawBSONDocument'>

API Documentation
-----------------
Expand Down
Loading
Loading