Skip to content

Commit 8028299

Browse files
committed
MD fixes
1 parent 45b92b0 commit 8028299

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

source/fundamentals/bson.txt

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Overview
1414
--------
1515

1616
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
1818
BSON is called **marshalling**, while the reverse process is called **unmarshalling**.
1919

2020
You should read this guide if you want to learn more about how the Go Driver
@@ -25,19 +25,15 @@ Data Types
2525
----------
2626

2727
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.
3029

31-
The ``D`` family contains four types:
30+
The Go Driver provides four main types for working with BSON data:
3231

3332
- ``D``: An ordered representation of a BSON document (slice)
3433
- ``M``: An unordered representation of a BSON document (map)
3534
- ``A``: An ordered representation of a BSON array
3635
- ``E``: A single element inside a D type
3736

38-
Of these, you can use the ``D`` and ``M`` types to
39-
build representations of BSON using native Go types.
40-
4137
The following example demonstrates how to construct a query filter using the
4238
``bson.D`` type to match documents with a ``quantity`` field value greater
4339
than 100:
@@ -46,21 +42,17 @@ than 100:
4642

4743
filter := bson.D{{"quantity", bson.D{{"$gt", 100}}}}
4844

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-
5745
For more information on how the Go Driver handles BSON data, see the
5846
`bson package API documentation
5947
<https://pkg.go.dev/go.mongodb.org/mongo-driver/bson>`_.
6048

6149
Struct Tags
6250
-----------
6351

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+
6456
You can modify the default marshalling and unmarshalling behavior of the Go Driver using
6557
**struct tags**, which are optional pieces of metadata attached to
6658
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:
213205

214206
- Sets the lowercase of the struct fields as the BSON field names
215207
- Includes an empty ``lastname`` field
216-
- Stores the ``Address`` field as a nested object
208+
- Stores the ``Address`` field as a nested value
217209

218210
Unmarshalling
219211
-------------
220212

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

225-
The ``Decode()`` method returns an ``error`` interface type which
216+
The ``Decode()`` method returns an ``error`` type which
226217
contains one of the following values:
227218

228219
- ``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:
291282
Unmarshalled Struct:
292283
{Category:plate Quantity:6}
293284

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+
294292
For more information on the marshalling and unmarshalling methods used with the
295293
``Cursor`` type, see the `Cursor API documentation
296294
<https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Cursor>`_

0 commit comments

Comments
 (0)