移除欄位

說明

從前一階段產生的文件中移除欄位。

產生的文件會包含前一階段的所有欄位,但指定要移除的欄位除外。

範例

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

Web

const results = await execute(db.pipeline()
  .collection("cities")
  .removeFields("population", "location.state"));
Swift
let results = try await db.pipeline()
  .collection("cities")
  .removeFields(["population", "location.state"])
  .execute()

Kotlin

val results = db.pipeline()
    .collection("cities")
    .removeFields("population", "location.state")
    .execute()

Java

Task<Pipeline.Snapshot> results = db.pipeline()
        .collection("cities")
        .removeFields("population", "location.state")
        .execute();
Python
results = (
    client.pipeline()
    .collection("cities")
    .remove_fields("population", "location.state")
    .execute()
)
Java
Pipeline.Snapshot results =
    firestore
        .pipeline()
        .collection("cities")
        .removeFields("population", "location.state")
        .execute()
        .get();

行為

移除巢狀欄位

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(...) 的巢狀或頂層欄位不存在於文件中,階段就不會編輯該欄位的文件。其他現有欄位仍會移除。