@@ -15,13 +15,12 @@ Work with BSON
1515Overview
1616--------
1717
18- In this guide, you can learn about how the Go Driver handles conversions
18+ In this guide, you can learn about how the {+driver-short+} handles conversions
1919between BSON and Go types. The process of converting a Go type to
2020BSON is called **marshalling**, while the reverse process is called **unmarshalling**.
2121
22- You should read this guide if you want to learn more about how the Go Driver
23- represents BSON data or need to adjust default marshalling
24- and unmarshalling behaviors.
22+ The following sections explain how the {+driver-short+} represents BSON data
23+ and how you can adjust default marshalling and unmarshalling behaviors.
2524
2625.. _golang-bson-types:
2726
@@ -31,22 +30,22 @@ Data Types
3130MongoDB stores documents in a binary representation called :manual:`BSON
3231</reference/bson-types/>` that allows for easy and flexible data processing.
3332
34- The Go Driver provides four main types for working with BSON data:
33+ The {+driver-short+} provides four main types for working with BSON data:
3534
3635- ``D``: An ordered representation of a BSON document (slice)
3736- ``M``: An unordered representation of a BSON document (map)
3837- ``A``: An ordered representation of a BSON array
3938- ``E``: A single element inside a D type
4039
41- The following example demonstrates how to construct a query filter using the
40+ The following example demonstrates how to construct a query filter by using the
4241``bson.D`` type to match documents with a ``quantity`` field value greater
4342than 100:
4443
4544.. code-block:: go
4645
4746 filter := bson.D{{"quantity", bson.D{{"$gt", 100}}}}
4847
49- To learn more about how the Go Driver handles BSON data, see the
48+ To learn more about how the {+driver-short+} handles BSON data, see the
5049`bson package API documentation <{+api+}/bson>`__.
5150
5251.. _golang-struct-tags:
@@ -55,15 +54,12 @@ Struct Tags
5554-----------
5655
5756In Go, a **struct** is a collection of data fields with declared data
58- types. The Go Driver can marshal/unmarshal structs and other native Go
59- types to/from BSON using a `configurable codec system <{+api+}/bson/bsoncodec>`_.
60-
61- You can modify the default marshalling and unmarshalling behavior of the Go Driver using
62- **struct tags**, which are optional pieces of metadata attached to
63- struct fields. The most common use of struct tags is for specifying the
64- field name in the BSON document that corresponds to the struct field.
57+ types. You can modify the default marshalling and unmarshalling behavior of
58+ a struct field by using **struct tags**, which are optional pieces of metadata
59+ attached to struct fields. The most common use of struct tags is for specifying
60+ the field name in the BSON document that corresponds to the struct field.
6561The following table describes the additional struct tags that you can
66- use with the Go Driver :
62+ use in the {+driver-short+} :
6763
6864.. list-table::
6965 :widths: 25 75
@@ -90,22 +86,22 @@ use with the Go Driver:
9086 - If the field type is a struct or map field, the field will be
9187 flattened when marshalling and unflattened when unmarshalling.
9288
93- Without additional instruction from struct tags, the Go
94- Driver will marshal structs using the following rules:
89+ If you don't specify struct tags, the {+driver-short+} marshals structs by using
90+ the following rules:
9591
96- #. The Go Driver only marshals and unmarshals exported fields.
92+ #. The driver only marshals and unmarshals exported fields.
9793
98- #. The Go Driver generates BSON key using the lowercase of the
94+ #. The driver generates a BSON key by using the lowercase of the
9995 corresponding struct field.
10096
101- #. The Go Driver marshals embedded struct fields as subdocuments.
102- Each key is the lowercase of the field's type.
97+ #. The driver marshals embedded struct fields as subdocuments. Each key
98+ is the lowercase of the field's type.
10399
104- #. The Go Driver marshals a pointer field as the underlying type if the
105- pointer is non-nil. If the pointer is nil, the driver marshals it as a BSON null
100+ #. The driver marshals a pointer field as the underlying type if the pointer
101+ is non-nil. If the pointer is nil, the driver marshals it as a BSON null
106102 value.
107103
108- #. When unmarshalling, the Go Driver follows `these D/M type mappings
104+ #. When unmarshalling, the {+driver-short+} follows `these D/M type mappings
109105 <{+api+}/bson#hdr-Native_Go_Types>`_
110106 for fields of type ``interface{}``. The driver unmarshals BSON documents
111107 unmarshalled into an ``interface{}`` field as a ``D`` type.
@@ -115,7 +111,7 @@ Driver will marshal structs using the following rules:
115111 .. tab:: Struct Tags
116112 :tabid: struct-tags
117113
118- The following example demonstrates how the Go Driver marshals a
114+ The following example demonstrates how the {+driver-short+} marshals a
119115 struct with various struct tags:
120116
121117 .. code-block:: go
@@ -164,8 +160,8 @@ Driver will marshal structs using the following rules:
164160 .. tab:: No Struct Tags
165161 :tabid: no-struct-tags
166162
167- The following example demonstrates how the Go Driver marshals a
168- struct without any struct tags:
163+ The following example demonstrates how the {+driver-short+} marshals
164+ a struct without any struct tags:
169165
170166 .. code-block:: go
171167
@@ -213,25 +209,42 @@ Driver will marshal structs using the following rules:
213209BSON Options
214210------------
215211
216- You can set ``BSONOptions`` to specify how
217- the driver marshals and unmarshals BSON on your collection, database, or
218- client.
212+ You can specify BSON options to adjust the marshalling and unmarshalling behavior of
213+ your ``Client`` instance. To set BSON options on your ``Client``, create and configure
214+ a ``BSONOptions`` instance.
215+
216+ This example performs the following actions:
217+
218+ - Creates a ``BSONOptions`` instance by configuring the following settings:
219+
220+ - Sets the ``UseJSONStructTags`` field to ``true``, which instructs the driver
221+ to use the ``"json"`` struct tag if a ``"bson"`` struct tag is not specified
222+ - Sets the ``OmitZeroStruct`` field to ``true``, which instructs the driver
223+ to marshal ``nil`` Go slices as empty BSON arrays
219224
220- The following example demonstrates how to set ``BSONOptions`` for
221- the ``movies`` collection:
225+ - Passes the ``BSONOptions`` instance to the ``SetBSONOptions()`` helper method to specify
226+ a ``ClientOptions`` instance
227+ - Creates a ``Client`` to apply the specified BSON marshalling and unmarshalling behavior
222228
223229.. code-block:: go
224- :copyable: false
225230
226231 bsonOpts := &options.BSONOptions {
227232 UseJSONStructTags: true,
233+ OmitZeroStruct: true,
228234 }
229- coll := client.Database("sample_mflix")
230- .Collection("movies",bsonOpts)
235+
236+ clientOpts := options.Client().
237+ ApplyURI("<connection string>").
238+ SetBSONOptions(bsonOpts)
239+
240+ client, err := mongo.Connect(context.TODO(), clientOpts)
231241
232242.. tip::
233243
234- To learn more about ``BSONOptions``, see the `API Documentation
244+ To learn more about the ``BSONOptions`` type, see the
245+ `BSONOptions API documentation <{+api+}/mongo/options#BSONOptions>`__.
246+ For an example that specifies a ``BSONOptions`` instance and creates a client with
247+ these options, see the `Connect() BSONOptions example
235248 <{+api+}/mongo#example-Connect-BSONOptions>`__.
236249
237250.. _golang-bson-unmarshalling:
@@ -284,9 +297,8 @@ operation:
284297The ``Cursor`` type also uses the ``All()`` method, which unmarshals all
285298documents stored in the cursor into an array at the same time.
286299
287- The ``bson`` package includes a family of
288- ``Marshal()`` and ``Unmarshal()`` methods that work with BSON-encoded data
289- of ``[]byte`` type.
300+ The ``bson`` package includes a family of ``Marshal()`` and ``Unmarshal()``
301+ methods that work with BSON-encoded data of ``[]byte`` type.
290302
291303The following code demonstrates how you can unmarshal BSON back into a
292304user-defined struct by using methods from the ``bson`` package:
0 commit comments