@@ -14,7 +14,7 @@ Overview
14
14
--------
15
15
16
16
In this guide, you can learn about how the Go Driver handles conversions
17
- between BSON and Go objects . The process of converting a Go object to
17
+ between BSON and Go values . The process of converting a Go value to
18
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
@@ -25,19 +25,15 @@ Data Types
25
25
----------
26
26
27
27
MongoDB stores documents in a binary representation called :manual:`BSON
28
- </reference/bson-types/>` that allows for easy and flexible data processing. The Go
29
- Driver represents BSON data using two families of types: ``D`` and ``Raw``.
28
+ </reference/bson-types/>` that allows for easy and flexible data processing.
30
29
31
- The ``D`` family contains four types:
30
+ The Go Driver provides four main types for working with BSON data :
32
31
33
32
- ``D``: An ordered representation of a BSON document (slice)
34
33
- ``M``: An unordered representation of a BSON document (map)
35
34
- ``A``: An ordered representation of a BSON array
36
35
- ``E``: A single element inside a D type
37
36
38
- Of these, you can use the ``D`` and ``M`` types to
39
- build representations of BSON using native Go types.
40
-
41
37
The following example demonstrates how to construct a query filter using the
42
38
``bson.D`` type to match documents with a ``quantity`` field value greater
43
39
than 100:
@@ -46,21 +42,17 @@ than 100:
46
42
47
43
filter := bson.D{{"quantity", bson.D{{"$gt", 100}}}}
48
44
49
- You can use ``Raw`` types to validate and retrieve elements from a slice
50
- of bytes, which is useful if you don't want to unmarshal BSON into
51
- another type.
52
-
53
- In Go, a **struct** is a collection of data fields with declared data
54
- types. The Go Driver can marshal/unmarshal structs and other native Go
55
- types to/from BSON using a `configurable codec system <https://pkg.go.dev/go.mongodb.org/mongo-driver/bson/bsoncodec>`_.
56
-
57
45
For more information on how the Go Driver handles BSON data, see the
58
46
`bson package API documentation
59
47
<https://pkg.go.dev/go.mongodb.org/mongo-driver/bson>`_.
60
48
61
49
Struct Tags
62
50
-----------
63
51
52
+ In Go, a **struct** is a collection of data fields with declared data
53
+ 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>`_.
55
+
64
56
You can modify the default marshalling and unmarshalling behavior of the Go Driver using
65
57
**struct tags**, which are optional pieces of metadata attached to
66
58
struct fields. The most common use of struct tags is for specifying the
@@ -213,16 +205,15 @@ Driver will marshal structs using the following rules:
213
205
214
206
- Sets the lowercase of the struct fields as the BSON field names
215
207
- Includes an empty ``lastname`` field
216
- - Stores the ``Address`` field as a nested object
208
+ - Stores the ``Address`` field as a nested value
217
209
218
210
Unmarshalling
219
211
-------------
220
212
221
- You can unmarshal BSON documents using the ``Decode()`` method. When you
222
- execute either a ``FindOne()`` or ``Find()`` query, you can use the
223
- ``Decode()`` method on the return object to unmarshal the results.
213
+ You can unmarshal BSON documents by using the ``Decode()`` method on the
214
+ result of the ``FindOne`` method or any ``*mongo.Cursor`` instance.
224
215
225
- The ``Decode()`` method returns an ``error`` interface type which
216
+ The ``Decode()`` method returns an ``error`` type which
226
217
contains one of the following values:
227
218
228
219
- ``nil`` if a document matched your query, and there were no errors
@@ -291,6 +282,13 @@ The output of the preceding code should look like this:
291
282
Unmarshalled Struct:
292
283
{Category:plate Quantity:6}
293
284
285
+ .. note::
286
+
287
+ You can use the ``Raw`` type to retrieve elements from a BSON
288
+ document byte slice without unmarshalling it to a Go value. This can
289
+ be useful if you need to look up individual elements without
290
+ unmarshalling the entire BSON document.
291
+
294
292
For more information on the marshalling and unmarshalling methods used with the
295
293
``Cursor`` type, see the `Cursor API documentation
296
294
<https://pkg.go.dev/go.mongodb.org/
[email protected] /mongo#Cursor>`_
0 commit comments