Undefined Values
On this page本页内容
Overview概述
In this guide, you can learn to control how the driver serializes 在本指南中,您可以学习控制驱动程序如何序列化undefined
values. undefined
的值。By default, the driver serializes 默认情况下,驱动程序在写入操作期间将undefined
values as null
values during write operations.undefined
的值序列化为null
值。
Ignore Undefined Values忽略未定义的值
To make the driver ignore fields with 若要使驱动程序在序列化期间忽略具有undefined
values during serialization, set the ignoreUndefined
setting to true
. undefined
值的字段,请将ignoreUndefined
设置设置为true
。When you specify this setting, the driver does not serialize fields with 指定此设置时,驱动程序不会序列化具有undefined
values.undefined
值的字段。
The following example inserts two documents. 以下示例插入两个文档。The first insert operation has the 第一个插入操作的ignoreUndefined
setting set to true
, so the driver does not serialize the salesTax
field in that operation. ignoreUndefined
设置设置为true
,因此驱动程序不会序列化该操作中的salesTax
字段。The second operation inserts a document that has the 第二个操作插入一个salesTax
field with a null
value:salesTax
字段为null
值的文档:
await myColl.insertOne(
{
state: "Montana",
salesTax: undefined,
},
{ ignoreUndefined: true }
);
await myColl.insertOne({
state: "New Hampshire",
salesTax: undefined,
});
The documents appear in the collection as follows:文档显示在集合中,如下所示:
{
_id: ...,
state: "Montana",
},
{
_id: ...,
state: "New Hampshire",
salesTax: null
}
Set the Scope for Serializing Undefined Values设置序列化未定义值的作用域
You can specify the 可以在以下级别指定ignoreUndefined
setting at the following levels:ignoreUndefined
设置:
The client level客户端级别The database level数据库级别The collection level集合级别The operation level操作级别
The ignoreUndefined
setting automatically applies to the scope of the object instance in which you specified it, as well as any other objects created from that instance.ignoreUndefined
设置自动应用于指定它的对象实例的作用域,以及从该实例创建的任何其他对象。
For example, if you set the 例如,如果在实例化数据库对象时设置了ignoreUndefined
setting when instantiating a database object, any collection instance created from that object inherits the setting. ignoreUndefined
设置,则从该对象创建的任何集合实例都会继承该设置。Furthermore, any operations that you call on that collection instance also inherit the setting.此外,您在该集合实例上调用的任何操作也会继承该设置。
The following example performs an find-and-update operation that inherits the 以下示例执行查找和更新操作,该操作从ignoreUndefined
setting from the myDB
database object. myDB
数据库对象继承ignoreUndefined
设置。This operation does not produce any data changes because the driver ignores the 此操作不会产生任何数据更改,因为驱动程序忽略gasTax
field:gasTax
字段:
const myDB = client.db("test", { ignoreUndefined: true });
//The collection inherits the ignoreUndefined setting集合继承ignoreUndefined设置
const myColl = myDB.collection("states");
//Any write operation will not serialize undefined values任何写入操作都不会序列化未定义的值
await myColl.findOneAndUpdate(
{ state: "Georgia" },
{ $set: { gasTax: undefined } }
);
You can specify the 您可以在任何级别再次指定ignoreUndefined
setting again at any level to override any inherited settings.ignoreUndefined
设置,以覆盖任何继承的设置。
For example, if you set 例如,如果在集合对象上将ignoreUndefined
to true
on your collection object, you can override the setting in individual write operations that you execute on that collection.ignoreUndefined
设置为true
,则可以在对该集合执行的各个写入操作中覆盖该设置。
const myColl = myDB.collection("states", { ignoreUndefined: true });
//The insert operation will not serialize undefined values插入操作不会序列化未定义的值
await myColl.insertOne({
state: "South Dakota",
capitalGainsTax: undefined,
});
//The insert operation will serialize undefined values插入操作将序列化未定义的值
await myColl.insertOne(
{ state: "Texas", capitalGainsTax: undefined },
{ ignoreUndefined: false }
);