-
Notifications
You must be signed in to change notification settings - Fork 20
DOCSP-46685: BSON #159
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
DOCSP-46685: BSON #159
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
.. _pymongo-bson: | ||
|
||
==== | ||
BSON | ||
==== | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to create BSON documents, read BSON from a file, | ||
and write BSON to a file by using {+driver-short+}. | ||
|
||
**BSON**, or Binary JSON, is the data format that MongoDB uses to organize | ||
and store data. This data format includes all JSON data structure types and | ||
adds support for types including dates, different size integers, ObjectIds, and | ||
binary data. You can use BSON documents in your {+language+} application by including the | ||
`bson <{+api-root+}bson/index.html>`__ package. For a complete list of supported types, see the | ||
:manual:`BSON Types </reference/bson-types>` server manual page. | ||
|
||
The code samples in this guide use the following BSON document as an example: | ||
|
||
.. code-block:: none | ||
|
||
{ | ||
"address" : { | ||
"street" : "Pizza St", | ||
"zipcode" : "10003" | ||
}, | ||
"coord" : [-73.982419, 41.579505] | ||
"cuisine" : "Pizza", | ||
"name" : "Mongo's Pizza" | ||
} | ||
|
||
Create a BSON Document | ||
---------------------- | ||
|
||
You can create a BSON document by using the same notation you use to create a | ||
dictionary in {+language+}. {+driver-short+} automatically converts {+language+} dictionaries | ||
into BSON documents when inserting them into a collection. | ||
|
||
The following example creates a BSON document that | ||
represents the preceding sample BSON document: | ||
|
||
.. code-block:: python | ||
|
||
document = { | ||
"address": { | ||
"street": "Pizza St", | ||
"zipcode": "10003" | ||
}, | ||
"coord": [-73.982419, 41.579505], | ||
"cuisine": "Pizza", | ||
"name": "Mongo's Pizza" | ||
} | ||
|
||
Change a BSON Document | ||
---------------------- | ||
|
||
You can modify the contents of a BSON document by using the same notation you use to modify | ||
a dictionary in {+language+}. The following example makes three changes to the previous | ||
BSON document: | ||
|
||
1. Adds a new field, ``restaurant_id``, with the value ``12345`` | ||
#. Removes the ``cuisine`` field | ||
#. Sets the value of the ``name`` field to ``"Mongo's Pizza Place"`` | ||
|
||
.. code-block:: python | ||
|
||
document["restaurant_id"] = 12345 | ||
del document["cuisine"] | ||
document["name"] = "Mongo's Pizza Place" | ||
|
||
Write BSON to a File | ||
-------------------- | ||
|
||
To write BSON data to a file, open a file stream in write-binary mode on the output file. | ||
Then, write each document to the output file. Ensure that documents are encoded in BSON | ||
format by using the ``BSON.encode()`` method. | ||
|
||
The following example writes the sample BSON document to ``file.bson``: | ||
|
||
.. code-block:: python | ||
|
||
import bson | ||
|
||
with open("file.bson", "w") as file: | ||
file.write(BSON.encode(document)) | ||
|
||
Read BSON from a File | ||
--------------------- | ||
|
||
To read BSON documents from a file, open a file stream in read-binary mode on the input | ||
file. Then, decode the documents from BSON format as you read them by using the ``BSON.decode()`` | ||
method. | ||
|
||
The following example reads the sample BSON document from ``file.bson``: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: | ||
:language: python | ||
|
||
with open("file.bson", "rb") as file: | ||
data = file.read() | ||
document = BSON(data).decode() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
print(document) | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
{"address": {"street": "Pizza St", "zipcode": "10003"}, "coord": [-73.982419, 41.579505], "cuisine": "Pizza", "name": "Mongo's Pizza"} | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about any of the methods or types discussed in this | ||
guide, see the `bson <{+api-root+}bson/index.html>`__ API documentation. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
bson.encode(document)
notBSON.encode()