@@ -52,13 +52,13 @@ represents the preceding sample BSON document:
52
52
.. code-block:: python
53
53
54
54
document = {
55
- ' address' : {
56
- ' street': ' Pizza St' ,
57
- ' zipcode': ' 10003'
55
+ " address" : {
56
+ " street": " Pizza St" ,
57
+ " zipcode": " 10003"
58
58
},
59
- ' coord' : [-73.982419, 41.579505],
60
- ' cuisine': ' Pizza' ,
61
- ' name': ' Mongo's Pizza'
59
+ " coord" : [-73.982419, 41.579505],
60
+ " cuisine": " Pizza" ,
61
+ " name": " Mongo's Pizza"
62
62
}
63
63
64
64
Change a BSON Document
@@ -70,39 +70,36 @@ BSON document:
70
70
71
71
1. Adds a new field, ``restaurant_id``, with the value ``12345``
72
72
#. Removes the ``cuisine`` field
73
- #. Sets the value of the ``name`` field to ``' Mongo's Pizza Place' ``
73
+ #. Sets the value of the ``name`` field to ``" Mongo's Pizza Place" ``
74
74
75
75
.. code-block:: python
76
76
77
- document[' restaurant_id' ] = 12345
78
- del document[' cuisine' ]
79
- document[' name' ] = ' Mongo's Pizza Place'
77
+ document[" restaurant_id" ] = 12345
78
+ del document[" cuisine" ]
79
+ document[" name" ] = " Mongo's Pizza Place"
80
80
81
81
Write BSON to a File
82
82
--------------------
83
83
84
- To write BSON data to a file, perform the following steps:
85
-
86
- 1. Fetch the documents you want to write to a file.
87
- #. Open a file stream on the file to write to in write-binary mode.
88
- #. Write each document to the output file. Ensure that documents are encoded to BSON
89
- format by using the ``BSON.encode()`` method.
84
+ To write BSON data to a file, open a file stream to 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.
90
87
91
88
The following example writes the sample BSON document to ``file.bson``:
92
89
93
90
.. code-block:: python
94
91
95
92
import bson
96
93
97
- with open(' file.bson', 'w' ) as file:
94
+ with open(" file.bson", "w" ) as file:
98
95
file.write(BSON.encode(document))
99
96
100
97
Read BSON from a File
101
98
---------------------
102
99
103
- To read BSON documents from a file, open a file stream on the file to read from in
104
- read-binary mode, then read the documents from the file. Ensure that the documents are
105
- decoded from BSON format by using the ``BSON.decode()`` method.
100
+ To read BSON documents from a file, open a file stream in read-binary mode on the input
101
+ file. Then, decode the documents from BSON format as you read them by using the ``BSON.decode()``
102
+ method.
106
103
107
104
The following example reads the sample BSON document from ``file.bson``:
108
105
@@ -112,20 +109,18 @@ The following example reads the sample BSON document from ``file.bson``:
112
109
.. input::
113
110
:language: python
114
111
115
- with open(' file.bson', 'rb' ) as file:
112
+ with open(" file.bson", "rb" ) as file:
116
113
data = file.read()
117
114
document = BSON(data).decode()
118
115
print(document)
119
116
120
117
.. output::
121
118
:visible: false
122
119
123
- {' address' : {' street': ' Pizza St', ' zipcode': ' 10003' }, ' coord' : [-73.982419, 41.579505], ' cuisine': ' Pizza', ' name' : "Mongo's Pizza"}
120
+ {" address" : {" street": " Pizza St", " zipcode": " 10003" }, " coord" : [-73.982419, 41.579505], " cuisine": " Pizza", " name" : "Mongo's Pizza"}
124
121
125
122
API Documentation
126
123
-----------------
127
124
128
125
To learn more about any of the methods or types discussed in this
129
- guide, see the following API documentation:
130
-
131
- - `bson <{+api-root+}bson/index.html>`__
126
+ guide, see the `bson <{+api-root+}bson/index.html>`__ API documentation.
0 commit comments