Firebase Firestore更新子集合文档
我想更新通过使用flifter发送组查询得到的子集合文档。根据我目前对组查询的理解我并不真正知道子集合的父集合 为此,我需要父文档的文档id。更新查询将如下所示:Firebase Firestore更新子集合文档,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我想更新通过使用flifter发送组查询得到的子集合文档。根据我目前对组查询的理解我并不真正知道子集合的父集合 为此,我需要父文档的文档id。更新查询将如下所示: collection(collectionName) .document(parentDocumentId) .collection(subCollectionName) .document(subCollectionDocumentId) .updateData(someMapWithData); 是否需要在子集合文档
collection(collectionName)
.document(parentDocumentId)
.collection(subCollectionName)
.document(subCollectionDocumentId)
.updateData(someMapWithData);
是否需要在子集合文档中保存parentDocumentId才能执行此更新,或者是否有其他方法执行此更新?如果要更新子集合中的文档,则需要顶部文档id和子集合中的文档id 是否需要在子集合文档中保存parentDocumentId才能执行此更新,或者是否有其他方法执行此操作 不需要,但如果您有
parentDocumentId
,而没有子文档ID
,则需要查询以检索它:
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"});
});
});
我明白了。尽管与您的建议相比,在子集合的文档中保存parentId会更省事,也可能更经济高效?在firestore中,您可能有“集合->文档”,而在文档中,您可以有一个子集合,其中包含多个文档。。多个文档不能具有相同的文档id。因此,最后,您必须执行查询才能更新数据。如果在这些多个文档中将parentId保存为字段(pID:parentId),则需要执行查询,如示例中所示。在查询中,您正在执行搜索,因此当我将parentId保存在子集合的文档中时,这将不是必需的,因为我可以执行以下查询:Firestore.instance.collection('path').document(parentId).集合('subCollection').document(documentId).updateData(…);在我的查询中,我在子集合中搜索,父ID我已经有了。但是正如我所说的,每个文档都必须有一个唯一的ID。在子集合中,您将有多个文档具有唯一的文档ID,在我的查询中,我在子集合中搜索文档ID。您打算如何获取此
文档(文档ID)
?如果我理解正确,您是说您将添加parentdocumentid作为子文档的文档id。但是,如果子集合中有多个文档,它们必须具有唯一的文档id,我很清楚每个文档都有自己的id。此时,我找到了文档的id在子集合中。我的问题是关于从下到上的搜索,以获取父文档的id。因此我认为最好的方法是将父文档的id作为字段传递给它的所有“子文档”。