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