简体   繁体   English

有没有办法可以将数据添加到 firestore 集合的父文档和子文档中?

[英]Is there a way I can add data to parent document and child document of a collection in firestore?

I'm working on e-commerce for my college project.我正在为我的大学项目从事电子商务。 I have a Parent collection of 'Products' and inside 'Products' collection I have documents of the different company name.我有一个“产品”的父集合,在“产品”集合中我有不同公司名称的文档。 Inside Company document I have created Collection of Categories likes vegetables, pastries etc.在公司内部文件中,我创建了蔬菜、糕点等类别的集合。

I want to add the address key-value pair to the company name document.我想将地址键值对添加到公司名称文档中。 In a single query.在单个查询中。 To do this right now I'm using This query.为此,我现在正在使用此查询。

await firestoreInstance
    .collection("Products")
    .document(_productCompany.toUpperCase())
    .setData({"data": ""}).then((value) => firestoreInstance
            .collection("Products")
            .document(_productCompany.toUpperCase())
            .collection(_productCategory)
            .document(_productCategory +
                " " +
                _productCompany +
                " " +
                _productName)
            .setData({
          "DownloadUrl": "$_downloadURL",
          "CategoryName": "$_productCategory",
          "CompanyName": "$_productCompany",
          "ProductName": _productName,
          "PricePerQuantity": _productPricePerQuantity,
          "Available": true,
          "itemQuantityToCart": 0
        }));

Is there a way I can do It In a single query?有没有办法在单个查询中完成它?

If you have two documents to update (regardless of their relationship to each other), you will need to update them separately.如果您有两个文档要更新(无论它们之间的关系如何),则需要分别更新它们。 There aren't any shortcuts for this.这没有任何捷径可走。

If you want to update two documents atomically (at the same time), then you can do a batch write to commit both changes simultaneously.如果您想以原子方式(同时)更新两个文档,那么您可以进行批量写入以同时提交两个更改。 But you will still have to generate two document references and specify the data those documents should contain.但是您仍然需要生成两个文档引用并指定这些文档应包含的数据。

You can read more about this specifically for Flutter here .您可以在此处阅读有关 Flutter 的更多信息。 The entry point for a batch write starts with the batch() method.批量写入的入口点从batch()方法开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Firestore 编码中将集合添加到文档中? - How can i add a collection to a document in firestore coding? 如何将集合添加到文档Firestore - How to add Collection To Document Firestore 访问firestore中父集合的文档中的集合 - Accessing the collection inside a document of a parent collection in firestore Firestore - 如何在我的 state 中添加文档 ID 及其数据? - Firestore - How can I add the Document ID and their data in my state? 在单个 Vue 组件中从 Firestore 获取父文档和子文档数据 - Getting parent and child document data from Firestore in a single Vue component 如何在 Firebase Firestore 中找到文档的父集合? - How to find the parent collection to a document in Firebase Firestore? AWS Lambda + Firestore 数据库:add() 不在集合中插入文档数据 - AWS Lambda + Firestore database: add() not inserting document data in collection 无法在云 Firestore 集合中添加多个文档 - Can't add more than one document in the cloud firestore collection 如何将来自 firestore 数据库中特定集合的文档字段数据存储到 kotlin 中的字符串变量 - How can I store document field data from a particular collection in firestore database to a string variable in kotlin 如何将元素推送到 Firestore 文档集合中的数组? - How can I push elements to array in firestore document collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM