Skip to content

Commit ce72d3d

Browse files
authored
DOCSP-48090 OmitEmpty global flag (#477)
* DOCSP-48090 OmitEmpty global flag * build fail * netlify still failing * RR comments * RR comments * add link to the BSON options section * RR feedback
1 parent f1acd71 commit ce72d3d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

source/fundamentals/bson.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ use in the {+driver-short+}:
7474
- Description
7575

7676
* - ``omitempty``
77-
- The field will not be marshalled if it is set to the zero value
78-
corresponding to the field type.
77+
- The field will not be marshaled if it is set to the zero value
78+
corresponding to the field type. To globally prevent the driver from
79+
marshaling fields set to a zero value, you can set the ``OmitEmpty``
80+
BSON option. To learn more about how to set the ``OmitEmpty`` BSON option,
81+
see the example in the :ref:`BSON Options <golang-bson-options>`
82+
section of this page.
7983

8084
* - ``minsize``
8185
- If the field is type ``int64``, ``uint``, ``uint32``, or ``uint64`` and
@@ -211,6 +215,8 @@ the following rules:
211215
- Includes an empty ``lastname`` field
212216
- Stores the ``Address`` field as a nested value
213217

218+
.. _golang-bson-options:
219+
214220
BSON Options
215221
------------
216222

@@ -226,6 +232,8 @@ This example performs the following actions:
226232
to use the ``"json"`` struct tag if a ``"bson"`` struct tag is not specified
227233
- Sets the ``NilSliceAsEmpty`` field to ``true``, which instructs the driver
228234
to marshal ``nil`` Go slices as empty BSON arrays
235+
- Sets the ``OmitEmpty`` field to ``true``, which instructs the driver to globally omit
236+
empty values from the marshaled BSON, rather than set empty values to ``null``
229237

230238
- Passes the ``BSONOptions`` instance to the ``SetBSONOptions()`` helper method to specify
231239
a ``ClientOptions`` instance
@@ -236,6 +244,7 @@ This example performs the following actions:
236244
bsonOpts := &options.BSONOptions {
237245
UseJSONStructTags: true,
238246
NilSliceAsEmpty: true,
247+
OmitEmpty: true,
239248
}
240249

241250
clientOpts := options.Client().

source/whats-new.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ What's New
2626

2727
Learn what's new in:
2828

29+
* :ref:`Version 2.2 <golang-version-2.2>`
2930
* :ref:`Version 2.1 <golang-version-2.1>`
3031
* :ref:`Version 2.0 <golang-version-2.0>`
3132
* :ref:`Version 1.17 <golang-version-1.17>`
@@ -37,6 +38,19 @@ Learn what's new in:
3738
* :ref:`Version 1.12.1 <version-1.12.1>`
3839
* :ref:`Version 1.12 <version-1.12>`
3940

41+
.. _golang-version-2.2:
42+
43+
What's New in 2.2
44+
-----------------
45+
46+
The 2.2 {+driver-short+} release includes the following improvements
47+
and fixes:
48+
49+
- Adds the ``OmitEmpty`` BSON option to globally prevent empty values from
50+
being serialized. To learn more about setting the ``OmitEmpty`` global flag,
51+
see the example in the :ref:`BSON Options <golang-bson-options>` section of the
52+
BSON guide.
53+
4054
.. _golang-version-2.1:
4155

4256
What's New in 2.1

0 commit comments

Comments
 (0)