0

I have two questions about the firestore collection group.

My data structure is like the bellow different carts for different stores(multi-vendor app).

Users>userId1>Carts>storeId1>Items>productId1

                   >storeId2>Items>productId1
                                  >productId2

Users>userId2>Carts>storeId1>Items>productId1

Now whenever a product price is changed by the owner I want to update the cart of all users with that product. So If I make Items subcollection a collection group will there be any performance issue? I mean does a deeper collection group affect the performance? Or It doesn't matter how deep nesting subcollection it is the performance will depend only on the number of documents.

2nd question is the same documentId(productId1) will be in multiple Items subcollection. So when it will merge and act as a giant Items collection by indexing will there be any conflict for the same document id?

Rafiul
  • 1,560
  • 7
  • 19
  • Hi @Rafiul , have you checked my posted answer? Let me know if you have any questions or clarifications. Also, see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Marc Anthony B Jul 11 '22 at 21:49

1 Answers1

1

Making a nested subcollection should be no difference in performance at all. Each collection and subcollection, no matter the depth, should operate completely and independently of each other. The collection is simply identified by its path, which is a string.

The primary performance characteristic of Firestore is that queries scale with the size of the result set, not the size of the collection. So, you can have massively huge collections, but that will not impact the performance of your query. It stands to reason that adding a few extra components to the path of a collection does not affect performance at all.

For your second question, document IDs are meant to be unique, there will be no conflicts if your main document ID such as the userID does not have the same ID, Firestore will not allow same document ID within the same collection so that there will be no conflict when indexing your group collection.

e.g.

Users > userId1 > Carts > storeId1 > Items > productId1

Users > userId2 > Carts > storeId1 > Items > productId1

From the example above, indexing two documents with the same subcollection path but with a different main document (userID) will result in a different index and will not result in any conflicts.

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19