【问题标题】:How to add new field and update existing field simultaneously in cloud firestore?如何在 Cloud Firestore 中同时添加新字段和更新现有字段?
【发布时间】:2020-07-29 09:18:51
【问题描述】:

我想在文档中添加一个新字段,但该字段不存在并同时更新现有字段,我尝试使用 update 并设置两者,但它们都没有添加新字段,尽管它可以更新现有字段.

const userDocumentReference = admin.firestore().collection('users').doc(data['uid']);
    await userDocumentReference.update({
      'isVerified': 'pending', // old field
      'selfieUrl': data['selfieUrl'] // new field
    }, { merge: true });

【问题讨论】:

  • 您好,如何在不存在的文档中添加新字段并同时更新现有字段?如果文档不存在,现有字段在哪里?请说清楚。另请注意,mergeoption 将与 set() 一起使用,而不是 update()
  • 嗨,我的意思是该字段不存在,但文档存在。
  • 好的,感谢您的澄清和更新!使用update()method 绝对可以。 data['selfieUrl'] 到底是什么?您的现有代码是否有任何错误?
  • 嗨,删除 merge: true 后它正在工作。再次感谢:)
  • 另外,是否可以将生成的长图像 URL 存储在文档中,否则会降低性能?

标签: node.js firebase google-cloud-firestore


【解决方案1】:

要更新现有的 Firestore 文档,使用 update() 方法,如下应该可以工作:

const userDocumentReference = admin.firestore().collection('users').doc(data['uid']);

await userDocumentReference.update({
      'isVerified': 'pending', // old field
      'selfieUrl': data['selfieUrl'] // new field
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2018-08-26
    相关资源
    最近更新 更多