Skip to content

Commit 4469c13

Browse files
committed
undefined values page & reorganization (#679)
* DOCSP-29510: explain how driver handles undefined vals (#674) * DOCSP-29510: explain how driver handles undefined vals * make new subfolder for bson * tree fixes and redirect-- snooty stuff * fix version in redirect * fix highlight * JS PR fixes 1 * JS suggestion (cherry picked from commit 52daa59) * redirect file revert (cherry picked from commit 157f483) (cherry picked from commit f7c1609) (cherry picked from commit 82a8fff) (cherry picked from commit d2b072b) (cherry picked from commit d3b2847) (cherry picked from commit 5a50abe)
1 parent 71fd5c7 commit 4469c13

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ toc_landing_pages = [
88
"/fundamentals",
99
"/fundamentals/connection",
1010
"/fundamentals/crud",
11+
"/fundamentals/bson",
1112
"/usage-examples",
1213
]
1314
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"

source/fundamentals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ Fundamentals
1818
/fundamentals/monitoring
1919
/fundamentals/gridfs
2020
/fundamentals/encrypt-fields
21+
/fundamentals/bson
2122

2223
.. include:: /includes/fundamentals-sections.rst

source/fundamentals/bson.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.. _node-bson-control:
2+
3+
=============
4+
BSON Settings
5+
=============
6+
7+
.. toctree::
8+
:caption: BSON settings
9+
10+
/fundamentals/bson/undefined-values
11+
12+
.. contents:: On this page
13+
:local:
14+
:backlinks: none
15+
:depth: 1
16+
:class: singlecol
17+
18+
Overview
19+
--------
20+
21+
Learn how to configure your application's BSON serialization settings.
22+
The guides in this section describe the following topics:
23+
24+
- :ref:`Undefined Values <node-undefined-values>`: Control how the
25+
driver serializes undefined values
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
.. _node-undefined-values:
2+
3+
================
4+
Undefined Values
5+
================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn to control how the driver serializes
17+
``undefined`` values. By default, the driver serializes ``undefined`` values
18+
as ``null`` values during write operations.
19+
20+
.. _nodejs-specify-ignoreundefined:
21+
22+
Ignore Undefined Values
23+
-----------------------
24+
25+
To make the driver ignore fields with
26+
``undefined`` values during serialization, set the
27+
``ignoreUndefined`` setting to ``true``. When you specify this setting,
28+
the driver *does not* serialize fields with ``undefined`` values.
29+
30+
The following example inserts two documents. The first insert operation has
31+
the ``ignoreUndefined`` setting set to ``true``, so the driver does not
32+
serialize the ``salesTax`` field in that operation. The second operation
33+
inserts a document that has the ``salesTax`` field with a ``null`` value:
34+
35+
.. code-block:: javascript
36+
:emphasize-lines: 6
37+
38+
await myColl.insertOne(
39+
{
40+
state: "Montana",
41+
salesTax: undefined,
42+
},
43+
{ ignoreUndefined: true }
44+
);
45+
46+
await myColl.insertOne({
47+
state: "New Hampshire",
48+
salesTax: undefined,
49+
});
50+
51+
The documents appear in the collection as follows:
52+
53+
.. code-block:: javascript
54+
:copyable: false
55+
56+
{
57+
_id: ...,
58+
state: "Montana",
59+
},
60+
{
61+
_id: ...,
62+
state: "New Hampshire",
63+
salesTax: null
64+
}
65+
66+
.. _nodejs-ignoreundefined-scope:
67+
68+
Set the Scope for Serializing Undefined Values
69+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70+
71+
You can specify the ``ignoreUndefined`` setting at the following levels:
72+
73+
- The client level
74+
- The database level
75+
- The collection level
76+
- The operation level
77+
78+
The ``ignoreUndefined`` setting automatically applies to the scope of the
79+
object instance in which you specified it, as well as any other objects created
80+
from that instance.
81+
82+
For example, if you set the ``ignoreUndefined`` setting when
83+
instantiating a database object, any collection instance created from
84+
that object inherits the setting. Furthermore, any operations that you
85+
call on that collection instance also inherit the setting.
86+
87+
The following example performs an find-and-update operation that
88+
inherits the ``ignoreUndefined`` setting from the ``myDB`` database
89+
object. This operation does not produce any data changes because the
90+
driver ignores the ``gasTax`` field:
91+
92+
.. code-block:: javascript
93+
94+
const myDB = client.db("test", { ignoreUndefined: true });
95+
96+
// The collection inherits the ignoreUndefined setting
97+
const myColl = myDB.collection("states");
98+
99+
// Any write operation will not serialize undefined values
100+
await myColl.findOneAndUpdate(
101+
{ state: "Georgia" },
102+
{ $set: { gasTax: undefined } }
103+
);
104+
105+
You can specify the ``ignoreUndefined`` setting again at any level to
106+
override any inherited settings.
107+
108+
For example, if you set ``ignoreUndefined`` to ``true`` on your
109+
collection object, you can override the setting in individual write
110+
operations that you execute on that collection.
111+
112+
.. code-block:: javascript
113+
:emphasize-lines: 1, 12
114+
115+
const myColl = myDB.collection("states", { ignoreUndefined: true });
116+
117+
// The insert operation will not serialize undefined values
118+
await myColl.insertOne({
119+
state: "South Dakota",
120+
capitalGainsTax: undefined,
121+
});
122+
123+
// The insert operation will serialize undefined values
124+
await myColl.insertOne(
125+
{ state: "Texas", capitalGainsTax: undefined },
126+
{ ignoreUndefined: false }
127+
);

source/fundamentals/crud/write-operations/upsert.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ beforehand:
9191
{ name: "Deli Llama", address: "3 Nassau St" },
9292
...
9393
]
94+

source/includes/fundamentals-sections.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Fundamentals section:
1313
- :doc:`Monitor Driver Events </fundamentals/monitoring>`
1414
- :doc:`Store and Retrieve Large Files in MongoDB </fundamentals/gridfs>`
1515
- :doc:`Encrypt Fields from the Client </fundamentals/encrypt-fields>`
16+
- :doc:`Specify BSON Serialization Settings </fundamentals/bson>`

0 commit comments

Comments
 (0)