0
votes

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?

1

1 Answers

1
votes

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. The entry point for a batch write starts with the batch() method.