【问题标题】:How to update existing document in firestore using set method如何使用 set 方法更新 Firestore 中的现有文档
【发布时间】:2021-04-04 16:16:45
【问题描述】:

我有一个要更新的 Firestore 文档。这是我的代码 -

admin.firestore().collection('testResult').doc(`${testId}`).set(
            {
                name: userName,
                email: email,
            },
            { merge: true },
        );

我正在使用带有“merge: true”对象的“set”方法。因为我想创建一个新文档,如果它不存在并更新数据,如果它存在。

我尝试更新的文档正在被我发送的新属性重写,而不是在现有文档中附加数据。我读到使用{merge: true} 会将传入数据与当前数据合并,但这不起作用。我的整个文档正在被新数据替换。

这是我第一次创建文档时的 firestore 集合 -

当我更新它时,这是同一个文档 -

【问题讨论】:

  • 代码乍一看还不错。您可以编辑您的问题以显示此代码运行之前之后的文档截图吗?
  • 当然。我应该以前做过。
  • 你为什么不用update()来更新数据?
  • 因为如果集合不存在,那将会失败。如果尚未创建,我想创建一个新集合。如果集合已经存在,则更新它。

标签: javascript database firebase google-cloud-firestore


【解决方案1】:

update() 方法(或带有合并标志的 set())创建一个文档:

  1. 包含您指定的字段的值。
  2. 文档中已有的任何其他字段。

因此,对于您指定的任何字段,文档中该字段的任何现有值被覆盖。

鉴于这些知识,结果按预期工作。


如果您想将您指定的值附加到该字段的当前值,您需要use an atomic transaction 来读写文档。

如果您想存储多个用户名+电子邮件组合,您需要在 Array 字段中执行此操作。然后,您可以再次将新组合添加到交易中,或者(如果用户名+电子邮件的组合必须是唯一的)您可以use the atomic array-union operation

【讨论】:

    【解决方案2】:

    merge 不会创建新的FIELDS,它允许您添加尚不存在的新字段或选择性地更新特定字段。字段名是唯一的;在你的声明中:

    admin.firestore().collection('testResult').doc(`${testId}`).set(
                {
                    name: userName,
                    email: email,
                },
                { merge: true },
            );
    

    您正在指定字段nameemail,并且正如命令所说,它将这些字段设置为新值。如果你这样做了:

    admin.firestore().collection('testResult').doc(`${testId}`).set(
                {
                    anotherName: userName,
                    anotherEmail: email,
                },
                { merge: true },
            );
    

    ...它将添加这些字段(如“将新字段合并到文档中”)并将现有字段 nameemail 留在原地

    如果你做了:

    admin.firestore().collection('testResult').doc(`${testId}`).set(
                {
                    anotherName: userName,
                    anotherEmail: email,
                },
                { merge: false },
            );
    

    ...(注意合并:false)它会将 DOCUMENT 设置为

                {
                    anotherName: userName,
                    anotherEmail: email,
                },
    

    ...忽略现有字段。

    请记住,文档中的字段名是唯一的 - 您不会看到这样的文档:

                {
                    name: userName1,
                    email: email1,
                    name: userName2,
                    email: email2,
                    name: userName3,
                    email: email3,
                    name: userName4,
                    email: email4,
                }
    

    我强烈建议您多研究文档并准确了解 Firestore 文档的工作原理

    【讨论】:

      【解决方案3】:

      我按照 Frank van Puffelen 的建议进行操作。如果将来有人需要,这是正确的代码-

      admin
          .firestore()
          .collection('testResult')
          .doc(`${testId}`)
          .get()
          .then(doc => {
              if (doc.exists) {
                  admin
                      .firestore()
                      .collection('testResult')
                      .doc(`${testId}`)
                      .update({
                          data: admin.firestore.FieldValue.arrayUnion(
                              { userName, email }
                          ),
                      });
              } else {
                  admin
                      .firestore()
                      .collection('testResult')
                      .doc(`${testId}`)
                      .set({
                          data: [{ userName, email }],
                      });
                  }
              return;
          })
          .catch(error => console.log(error));
      
      

      首先我检查了文档是否存在。如果它不存在,那么我将创建一个新文档并保存一个包含我的值的数组的对象。如果它存在,那么我正在更新数组。

      【讨论】:

        猜你喜欢
        • 2020-11-26
        • 2021-02-05
        • 1970-01-01
        • 2020-11-14
        • 2020-10-03
        • 2023-04-05
        • 1970-01-01
        • 2020-08-09
        • 1970-01-01
        相关资源
        最近更新 更多