@@ -91,44 +91,61 @@ it back into a ``Restaurant`` object from the preceding example:
9191To learn more about retrieving documents from a collection, see the :ref:`pymongo-retrieve`
9292guide.
9393
94- Binary Fields
95- -------------
94+ .. _pymongo-serialization-binary-data:
95+
96+ Binary Data
97+ -----------
9698
9799In all versions of Python, {+driver-short+} encodes instances of the
98100`bytes <https://docs.python.org/3/library/stdtypes.html#bytes>`__ class
99101as binary data with subtype 0, the default subtype for binary data. In Python 3,
100102{+driver-short+} decodes these values to instances of the ``bytes`` class. In Python 2,
101- however, the driver decodes them to instances of the
103+ the driver decodes them to instances of the
102104`Binary <https://pymongo.readthedocs.io/en/4.11/api/bson/binary.html#bson.binary.Binary>`__
103105class with subtype 0.
104106
105- The following code examples use {+driver-short+} to insert a ``bytes`` instance
106- into MongoDB, and then find the instance.
107- In Python 2, the byte string is decoded to ``Binary``.
108- In Python 3, the byte string is decoded back to ``bytes``.
107+ The following code examples show how {+driver-short+} decodes instances of the ``bytes``
108+ class. Select the :guilabel:`Python 2` or :guilabel:`Python 3` tab to view the corresponding
109+ code.
109110
110111.. tabs::
111112
112- .. tab:: Python 2.7
113- :tabid: python-2
113+ .. tab:: Python 2
114+ :tabid: python2
114115
115- .. code-block:: python
116+ .. io-code-block::
117+ :copyable: true
116118
117- >>> import pymongo
118- >>> c = pymongo.MongoClient()
119- >>> c.test.bintest.insert_one().inserted_id
120- ObjectId('4f9086b1fba5222021000000')
121- >>> c.test.bintest.find_one()
122- {u'binary': Binary('this is a byte string', 0), u'_id': ObjectId('4f9086b1fba5222021000000')}
123-
124- .. tab:: Python 3.7
125- :tabid: python-3
119+ .. input::
120+ :language: python
121+
122+ from pymongo import MongoClient
123+
124+ client = MongoClient()
125+ client.test.test.insert_one({'binary': b'this is a byte string'})
126+ doc = client.test.test.find_one()
127+ print(doc)
128+
129+ .. output::
130+
131+ {u'_id': ObjectId('67afb78298f604a28f0247b4'), u'binary': Binary('this is a byte string', 0)}
132+
133+ .. tab:: Python 3
134+ :tabid: python3
135+
136+ .. io-code-block::
137+ :copyable: true
138+
139+ .. input::
140+ :language: python
141+
142+ from pymongo import MongoClient
143+
144+ client = MongoClient()
145+ client.test.test.insert_one({'binary': b'this is a byte string'})
146+ doc = client.test.test.find_one()
147+ print(doc)
126148
127- .. code-block:: python
149+ .. output::
128150
129- >>> import pymongo
130- >>> c = pymongo.MongoClient()
131- >>> c.test.bintest.insert_one({'binary': b'this is a byte string'}).inserted_id
132- ObjectId('4f9086b1fba5222021000000')
133- >>> c.test.bintest.find_one()
134- {'binary': b'this is a byte string', '_id': ObjectId('4f9086b1fba5222021000000')}
151+ {'_id': ObjectId('67afb78298f604a28f0247b4'), 'binary': b'this is a byte string'}
0 commit comments