@@ -25,6 +25,39 @@ binary data. You can use BSON documents in your {+language+} application by incl
25
25
`bson <{+api-root+}bson/index.html>`__ package. For a complete list of supported types, see the
26
26
:manual:`BSON Types </reference/bson-types>` server manual page.
27
27
28
+ BSON documents are stored in MongoDB collections in binary format, while {+driver-short+}
29
+ represents BSON documents as {+language+} dictionaries. {+driver-short+} automatically
30
+ converts {+language+} dictionaries into BSON documents when inserting them into a collection.
31
+ Likewise, when you retrieve a document from a collection, {+driver-short+} converts the BSON
32
+ document back into a {+language+} dictionary.
33
+
34
+ The following example shows a document in both dictionary and BSON formats. Use the
35
+ :guilabel:`Dictionary` or :guilabel:`BSON` tab to see the corresponding format:
36
+
37
+ .. tabs::
38
+
39
+ .. tab:: Dictionary
40
+ :tabid: dict
41
+
42
+ .. code-block:: python
43
+
44
+ {"hello": "world"}
45
+
46
+ .. tab:: BSON
47
+ :tabid: bson
48
+
49
+ .. code-block:: none
50
+
51
+ \x16\x00\x00\x00 # total document size
52
+ \x02 # 0x02 = type String
53
+ hello\x00 # field name
54
+ \x06\x00\x00\x00world\x00 # field value
55
+ \x00 # 0x00 = type EOO ("end of object")
56
+
57
+
58
+ Sample Data
59
+ ~~~~~~~~~~~
60
+
28
61
The code samples in this guide use the following BSON document as an example:
29
62
30
63
.. code-block:: none
@@ -43,10 +76,7 @@ Create a BSON Document
43
76
----------------------
44
77
45
78
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
79
+ dictionary in {+language+}. The following example creates a BSON document that
50
80
represents the preceding sample BSON document:
51
81
52
82
.. code-block:: python
@@ -65,7 +95,7 @@ Change a BSON Document
65
95
----------------------
66
96
67
97
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
98
+ a dictionary in {+language+}. The following example makes three changes to the sample
69
99
BSON document:
70
100
71
101
1. Adds a new field, ``restaurant_id``, with the value ``12345``
0 commit comments