移除字段

说明

从前一阶段生成的文档中移除字段。

生成的文档将包含上一个阶段的所有字段,但指定要移除的字段除外。

语法

Node.js

const results = await db.pipeline()
  .collection("/cities")
  .removeFields('population', 'location.state')
  .execute();

行为

移除嵌套字段

remove_fields 阶段遵循嵌套字段语法,并将从映射中移除键。

例如,如需从数据集中移除嵌套的州字段,请执行以下操作:

Node.js

await db.collection('cities').doc('SF').set({name: 'San Francisco', location: {country: 'USA', state: 'California'}});
await db.collection('cities').doc('TO').set({name: 'Toronto', location: {country: 'Canada', province: 'Ontario'}});

可使用以下流水线:

Node.js

const results = await db.pipeline()
  .collection("/cities")
  .removeFields('location.state')
  .execute();

这会生成以下文档:

{name: 'San Francisco', location: {country: 'USA'}}
{name: 'Toronto', location: {country: 'Canada', province: 'Ontario'}}

不支持移除数组中的元素。

针对不存在的字段进行移除

如果传递给 remove_fields 的嵌套字段或顶级字段在文档中不存在,则该阶段不会针对该字段修改文档。其他现有字段仍会被移除。