gpt4 book ai didi

firebase - Firestore更新子集合文档

转载 作者:行者123 更新时间:2023-12-03 04:49:06 25 4
gpt4 key购买 nike

我想通过使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com