|
| 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 | + ); |
0 commit comments