|
| 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