【问题标题】:Update Firebase document based on custom fields [duplicate]根据自定义字段更新 Firebase 文档 [重复]
【发布时间】:2019-10-28 16:08:28
【问题描述】:

我得到了以下 Firebase 文档:

{
  "customField1": "customValue1",
  "customField2": "customValue2",
  "fieldToChange: "valueToChange"
}

我想知道怎么做两个操作:

  1. 找到将“customField1”设置为“customValue1”的文档并将“valueToChange”更新为其他内容。

  2. 找到“customField1”设置为“customValue1”且“customField2”设置为“customValue2”的文档,并将“valueToChange”更新为其他内容。

我怎样才能做到这一点?

我只看到需要 docId 的选项:

const uploadCollection = this.db.collection<any>('uploads');
    return await uploadCollection.doc(docId).update({ 'fieldToChange': 'another value' });

【问题讨论】:

  • Firestore 中没有更新查询的概念。要更新文档,您需要知道该文档的完整路径。因此,如果您想更新与某个条件匹配的文档,您需要针对该条件执行查询,遍历所有生成的文档,并更新每个文档。见stackoverflow.com/questions/54541228/…

标签: javascript firebase google-cloud-firestore


【解决方案1】:

这可能是您正在寻找的某种形式:

var uploads = db.collection("uploads").where("customField1", "==", "customValue1");

uploads.get().then(querySnapshot => {
    querySnapshot.forEach(doc => {
        doc.ref.update({ 'fieldToChange': 'another value' });
    });
});

查看这里了解更多信息:https://firebase.google.com/docs/firestore/query-data/queries

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多