firebase - Firestore更新子集合文档

标签 firebase flutter dart google-cloud-firestore

我想通过使用Flutter发送组查询来更新我得到的子集合文档。就目前对组查询的理解而言,我并不真正知道子集合的父级。

为此,我需要父文档的文档ID。更新查询将如下所示:

collection(collectionName)
.document(parentDocumentId)
 .collection(subCollectionName)
  .document(subCollectionDocumentId)
   .updateData(someMapWithData);

是否有必要将parentDocumentId保存在子集合文档中才能进行此更新,或者还有另一种方法可以这样做?

最佳答案

如果要更新子集合内的文档,则需要顶部文档ID和子集合内的文档ID。

Is it necessary to save the parentDocumentId within the sub-collection document to be able to do such update or is there another way to do so?



不,不是必须的,但是如果您具有parentDocumentId,而您没有subDocumentId,那么您需要查询以检索它:
    Firestore.instance
        .collection("path")
        .document("docPath")
        .collection("subCollection")
        .where("name", isEqualTo: "john")
        .getDocuments()
        .then((res) {
      res.documents.forEach((result) {
        Firestore.instance
            .collection("path")
            .document("docPath")
            .collection("subCollection")
            .document(result.documentID)
            .updateData({"name": "martin"});
      });
    });

关于firebase - Firestore更新子集合文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61711134/

相关文章:

javascript - 使用firebase(React)将用户数据添加到数据库(Google身份验证)

Flutter GoRouter 不允许我将 int 值作为查询参数传递

flutter - 用于Unicode 13表情符号的RegEx

android - 应用程序中的生物识别身份验证和 oauth2

dart - 在 Dart 中必须 element.html ("") 出现在 body 标签的上下文中吗?

javascript - 为什么 req.cookies 未定义?

javascript - 在 Ember.js 应用程序中实现 Firebase simpleLogin 电子邮件和密码

dart - dartanalyzer在入门教程中报告错误

flutter - 从无状态小部件调用到有状态小部件时,变量返回null吗?

java - 我想做一点计算并将值传递给另一个 Activity 上的 TextView