【问题标题】:How to add a new field in all the firebase documents?如何在所有 Firebase 文档中添加新字段?
【发布时间】:2021-11-13 22:48:37
【问题描述】:

我在 App Store 上有一个 firebase 应用程序。在 firebase 的用户集合中,我有一堆字段,例如 first_name、last_name 等。我想在所有现有文档中添加另一个字段(年龄)。我怎样才能做到这一点?如果我不更新它,应用程序会给出错误,指出文档中不存在该字段。

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    这没什么特别的:

    1. 阅读所有文件。
    2. 循环遍历结果。
    3. 更新它们。

    由于您使用 Cloud Functions 进行了标记,因此在 Node.js 中这将类似于:

    const usersRef = db.collection('users');
    const snapshot = await usersRef.get();
    await Promise.all(snapshot.docs.map(doc => doc.ref.update({age: 42})));
    

    这是基于 Firebase 文档中的这些链接:

    您可能还想看看:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      相关资源
      最近更新 更多