1
- =================
2
- Working with BSON
3
- =================
1
+ ==============
2
+ Work with BSON
3
+ ==============
4
4
5
5
.. default-domain:: mongodb
6
6
@@ -15,7 +15,7 @@ Overview
15
15
16
16
In this guide, you can learn about how the Go Driver handles conversions
17
17
between BSON and Go values. The process of converting a Go value to
18
- BSON is called **marshalling**, while the reverse process is called **unmarshalling**.
18
+ BSON is called **marshalling**, while the reverse process is called **unmarshalling**.
19
19
20
20
You should read this guide if you want to learn more about how the Go Driver
21
21
represents BSON data or need to adjust default marshalling
@@ -39,19 +39,18 @@ The following example demonstrates how to construct a query filter using the
39
39
than 100:
40
40
41
41
.. code-block:: go
42
-
42
+
43
43
filter := bson.D{{"quantity", bson.D{{"$gt", 100}}}}
44
44
45
45
For more information on how the Go Driver handles BSON data, see the
46
- `bson package API documentation
47
- <https://pkg.go.dev/go.mongodb.org/mongo-driver/bson>`_.
46
+ `bson package API documentation <{+api+}/bson>`__.
48
47
49
48
Struct Tags
50
49
-----------
51
50
52
51
In Go, a **struct** is a collection of data fields with declared data
53
52
types. The Go Driver can marshal/unmarshal structs and other native Go
54
- types to/from BSON using a `configurable codec system <https://pkg.go.dev/go.mongodb.org/mongo-driver /bson/bsoncodec>`_.
53
+ types to/from BSON using a `configurable codec system <{+api+} /bson/bsoncodec>`_.
55
54
56
55
You can modify the default marshalling and unmarshalling behavior of the Go Driver using
57
56
**struct tags**, which are optional pieces of metadata attached to
@@ -79,11 +78,11 @@ use with the Go Driver:
79
78
80
79
* - ``truncate``
81
80
- If the field type is a non-float numeric type, BSON doubles
82
- unmarshalled into that field will be trucated at the decimal point.
81
+ unmarshalled into that field will be truncated at the decimal point.
83
82
84
83
* - ``inline``
85
84
- If the field type is a struct or map field, the field will be
86
- " flattened" when marshalling and "un-flattened" when unmarshalling.
85
+ flattened when marshalling and unflattened when unmarshalling.
87
86
88
87
Without additional instruction from struct tags, the Go
89
88
Driver will marshal structs using the following rules:
@@ -101,7 +100,7 @@ Driver will marshal structs using the following rules:
101
100
value.
102
101
103
102
#. When unmarshalling, the Go Driver follows `these D/M type mappings
104
- <https://pkg.go.dev/go.mongodb.org/mongo-driver /bson#hdr-Native_Go_Types>`_
103
+ <{+api+} /bson#hdr-Native_Go_Types>`_
105
104
for fields of type ``interface{}``. The driver unmarshals BSON documents
106
105
unmarshalled into an ``interface{}`` field as a ``D`` type.
107
106
@@ -120,7 +119,7 @@ Driver will marshal structs using the following rules:
120
119
City string
121
120
State string
122
121
}
123
-
122
+
124
123
type Student struct {
125
124
FirstName string `bson:"first_name,omitempty"`
126
125
LastName string `bson:"last_name,omitempty"`
@@ -138,8 +137,8 @@ Driver will marshal structs using the following rules:
138
137
139
138
.. code-block:: json
140
139
:copyable: false
141
-
142
- {
140
+
141
+ {
143
142
"_id" : ObjectId("..."),
144
143
"first_name" : "Arthur",
145
144
"street" : "1 Lakewood Way",
@@ -149,7 +148,7 @@ Driver will marshal structs using the following rules:
149
148
}
150
149
151
150
In this example, struct tags make the driver:
152
-
151
+
153
152
- Set custom BSON field names such as ``first_name``
154
153
- Omit the empty ``LastName`` field
155
154
- Flatten the nested struct and bring all fields up to the top
@@ -170,7 +169,7 @@ Driver will marshal structs using the following rules:
170
169
City string
171
170
State string
172
171
}
173
-
172
+
174
173
type Student struct {
175
174
FirstName string
176
175
LastName string
@@ -188,8 +187,8 @@ Driver will marshal structs using the following rules:
188
187
189
188
.. code-block:: json
190
189
:copyable: false
191
-
192
- {
190
+
191
+ {
193
192
"_id" : ObjectId("..."),
194
193
"firstname" : "Arthur",
195
194
"lastname" : "",
@@ -200,9 +199,9 @@ Driver will marshal structs using the following rules:
200
199
},
201
200
"age" : 8
202
201
}
203
-
202
+
204
203
Without struct tags, the driver:
205
-
204
+
206
205
- Sets the lowercase of the struct fields as the BSON field names
207
206
- Includes an empty ``lastname`` field
208
207
- Stores the ``Address`` field as a nested value
@@ -219,12 +218,12 @@ The ``Decode()`` method returns an ``error`` type which
219
218
contains one of the following values:
220
219
221
220
- ``nil`` if a document matched your query, and there were no errors
222
- retrieving and unmarshalling the document.
221
+ retrieving and unmarshalling the document.
223
222
- If the driver retrieved your document but could not unmarshal your result, the
224
223
``Decode()`` method returns the unmarshalling error.
225
224
- If there was an error retrieving your document during execution of the
226
225
``FindOne()`` method, the error propagates to the ``Decode()`` method and
227
- the ``Decode()`` method returns the error.
226
+ the ``Decode()`` method returns the error.
228
227
229
228
When used on the ``SingleResult`` type returned by the ``FindOne()``
230
229
method, ``Decode()`` can also return the ``ErrNoDocuments`` error if no
@@ -235,7 +234,7 @@ method to unmarshal and read the result of a simple ``FindOne()``
235
234
operation:
236
235
237
236
.. code-block:: go
238
-
237
+
239
238
coll := client.Database("school").Collection("students")
240
239
filter := bson.D{{"age", 8}}
241
240
@@ -248,21 +247,21 @@ operation:
248
247
The output of the preceding code should look like this:
249
248
250
249
.. code-block:: none
251
-
250
+
252
251
[{_id ObjectID("...")} {first_name Arthur} {street 1 Fern Way} {city Elwood City} {state PA} {age 8}]
253
252
254
253
The ``Cursor`` type also uses the ``All()`` method, which unmarshals all
255
254
documents stored in the cursor into an array at the same time.
256
255
257
256
The ``bson`` package includes a family of
258
257
``Marshal()`` and ``Unmarshal()`` methods that work with BSON-encoded data
259
- of ``[]byte`` type.
258
+ of ``[]byte`` type.
260
259
261
260
The following code demonstrates how you can unmarshal BSON back into a
262
261
user-defined struct by using methods from the ``bson`` package:
263
262
264
263
.. code-block:: go
265
-
264
+
266
265
type Item struct {
267
266
Category string
268
267
Quantity int32
@@ -279,21 +278,20 @@ user-defined struct by using methods from the ``bson`` package:
279
278
The output of the preceding code should look like this:
280
279
281
280
.. code-block:: none
282
-
281
+
283
282
Unmarshalled Struct:
284
283
{Category:plate Quantity:6}
285
284
286
285
.. note::
287
-
286
+
288
287
You can use the ``Raw`` type to retrieve elements from a BSON
289
288
document byte slice without unmarshalling it to a Go value. This can
290
289
be useful if you need to look up individual elements without
291
290
unmarshalling the entire BSON document.
292
291
293
292
For more information on the marshalling and unmarshalling methods used with the
294
- ``Cursor`` type, see the `Cursor API documentation
295
- <https://pkg.go.dev/go.mongodb.org/
[email protected] /mongo#Cursor>`_
293
+ ``Cursor`` type, see the `Cursor API documentation <{+api+}/mongo#Cursor>`__
296
294
297
295
For more information on the marshalling and unmarshalling methods in the
298
296
``bson`` package, see the `bson API documentation
299
- <
https://pkg.go.dev/go.mongodb.org/[email protected] /bson#hdr-Marshalling_and_Unmarshalling>`_
297
+ <{+api+} /bson#hdr-Marshalling_and_Unmarshalling>`_
0 commit comments