Skip to content

Commit c5b9b64

Browse files
committed
DOCSP-46685: BSON (#159)
(cherry picked from commit 11167f1)
1 parent d2137b7 commit c5b9b64

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

source/data-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Specialized Data Formats
2121
:titlesonly:
2222
:maxdepth: 1
2323

24+
BSON </data-formats/bson>
2425
Custom Types </data-formats/custom-types>
2526
Dates & Times </data-formats/dates-and-times>
2627
UUIDs </data-formats/uuid>
@@ -33,6 +34,7 @@ You can use several types of specialized data formats in your {+driver-short+}
3334
application. To learn how to work with these data formats, see the following
3435
sections:
3536

37+
- Learn how to work with BSON documents in the :ref:`pymongo-bson` guide.
3638
- Learn how to encode and decode custom types in the :ref:`pymongo-custom-types` guide.
3739
- Learn how to work with Python ``datetime`` objects in {+driver-short+} in the
3840
:ref:`pymongo-dates-times` guide.

source/data-formats/bson.txt

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
.. _pymongo-bson:
2+
3+
====
4+
BSON
5+
====
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Overview
16+
--------
17+
18+
In this guide, you can learn how to create BSON documents, read BSON from a file,
19+
and write BSON to a file by using {+driver-short+}.
20+
21+
**BSON**, or Binary JSON, is the data format that MongoDB uses to organize
22+
and store data. This data format includes all JSON data structure types and
23+
adds support for types including dates, different size integers, ObjectIds, and
24+
binary data. You can use BSON documents in your {+language+} application by including the
25+
`bson <{+api-root+}bson/index.html>`__ package. For a complete list of supported types, see the
26+
:manual:`BSON Types </reference/bson-types>` server manual page.
27+
28+
The code samples in this guide use the following BSON document as an example:
29+
30+
.. code-block:: none
31+
32+
{
33+
"address" : {
34+
"street" : "Pizza St",
35+
"zipcode" : "10003"
36+
},
37+
"coord" : [-73.982419, 41.579505]
38+
"cuisine" : "Pizza",
39+
"name" : "Mongo's Pizza"
40+
}
41+
42+
Create a BSON Document
43+
----------------------
44+
45+
You can create a BSON document by using the same notation you use to create a
46+
dictionary in {+language+}. {+driver-short+} automatically converts {+language+} dictionaries
47+
into BSON documents when inserting them into a collection.
48+
49+
The following example creates a BSON document that
50+
represents the preceding sample BSON document:
51+
52+
.. code-block:: python
53+
54+
document = {
55+
"address": {
56+
"street": "Pizza St",
57+
"zipcode": "10003"
58+
},
59+
"coord": [-73.982419, 41.579505],
60+
"cuisine": "Pizza",
61+
"name": "Mongo's Pizza"
62+
}
63+
64+
Change a BSON Document
65+
----------------------
66+
67+
You can modify the contents of a BSON document by using the same notation you use to modify
68+
a dictionary in {+language+}. The following example makes three changes to the previous
69+
BSON document:
70+
71+
1. Adds a new field, ``restaurant_id``, with the value ``12345``
72+
#. Removes the ``cuisine`` field
73+
#. Sets the value of the ``name`` field to ``"Mongo's Pizza Place"``
74+
75+
.. code-block:: python
76+
77+
document["restaurant_id"] = 12345
78+
del document["cuisine"]
79+
document["name"] = "Mongo's Pizza Place"
80+
81+
Write BSON to a File
82+
--------------------
83+
84+
To write BSON data to a file, open a file stream in write-binary mode on the output file.
85+
Then, write each document to the output file. Ensure that documents are encoded in BSON
86+
format by using the ``bson.encode()`` method.
87+
88+
The following example writes the sample BSON document to ``file.bson``:
89+
90+
.. code-block:: python
91+
92+
with open("file.bson", "w") as file:
93+
file.write(bson.encode(document))
94+
95+
Read BSON from a File
96+
---------------------
97+
98+
To read BSON documents from a file, open a file stream in read-binary mode on the input
99+
file. Then, decode the documents from BSON format as you read them by using the ``bson.decode()``
100+
method.
101+
102+
The following example reads the sample BSON document from ``file.bson``:
103+
104+
.. io-code-block::
105+
:copyable: true
106+
107+
.. input::
108+
:language: python
109+
110+
with open("file.bson", "rb") as file:
111+
data = file.read()
112+
document = bson.decode(data)
113+
print(document)
114+
115+
.. output::
116+
:visible: false
117+
118+
{"address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"}
119+
120+
API Documentation
121+
-----------------
122+
123+
To learn more about any of the methods or types discussed in this
124+
guide, see the `bson <{+api-root+}bson/index.html>`__ API documentation.

0 commit comments

Comments
 (0)