Skip to content

DOCSP-48090 OmitEmpty global flag #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions source/fundamentals/bson.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ use in the {+driver-short+}:
- Description

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

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

.. _golang-bson-options:

BSON Options
------------

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

- Passes the ``BSONOptions`` instance to the ``SetBSONOptions()`` helper method to specify
a ``ClientOptions`` instance
Expand All @@ -236,6 +244,7 @@ This example performs the following actions:
bsonOpts := &options.BSONOptions {
UseJSONStructTags: true,
NilSliceAsEmpty: true,
OmitEmpty: true,
}

clientOpts := options.Client().
Expand Down
14 changes: 14 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ What's New

Learn what's new in:

* :ref:`Version 2.2 <golang-version-2.2>`
* :ref:`Version 2.1 <golang-version-2.1>`
* :ref:`Version 2.0 <golang-version-2.0>`
* :ref:`Version 1.17 <golang-version-1.17>`
Expand All @@ -37,6 +38,19 @@ Learn what's new in:
* :ref:`Version 1.12.1 <version-1.12.1>`
* :ref:`Version 1.12 <version-1.12>`

.. _golang-version-2.2:

What's New in 2.2
-----------------

The 2.2 {+driver-short+} release includes the following improvements
and fixes:

- Adds the ``OmitEmpty`` BSON option to globally prevent empty values from
being serialized. To learn more about setting the ``OmitEmpty`` global flag,
see the example in the :ref:`BSON Options <golang-bson-options>` section of the
BSON guide.

.. _golang-version-2.1:

What's New in 2.1
Expand Down
Loading