0

I'm using Cloud Firestore with Flutter and am showing products to gift buyers for gift recipients.

I have a large list of products that I want to recommend to giftBuyers for giftRecipients. Because the list of products is large, I want to avoid copying it to each giftRecipient for each giftBuyer.

I want the products to only be shown once to a giftBuyer for a given giftRecipient. However, I want to be able to search for all of the products viewed by a giftBuyer for a giftRecipient in the future.

The schema I started using was:

  • products
    • product 1
      • merchantName <- Field
      • productName <- Field
      • giftBuyerCollection <-subcollection
        • giftBuyer A
          • giftRecipients <-subcollection
            • giftRecipient X
              • productViewed: true <-Field
            • giftRecipient Y
        • giftBuyer B
          • giftRecipients <-subcollection
            • giftRecipient X
            • giftRecipient Y
    • product 2

When I fetch products, I want to create a query along the lines of

pseudocode{
return all product documents where
.collection(giftBuyerCollection)
.doc(giftBuyer A)
.collection(giftRecipients)
.doc(giftRecipient X)
.field(productViewed does not exist)
}

Is this possible? If so, would it involve multipe queries? This seems like a use case for a collectionGroup but I am not certain how to structure it with the IDs.

The other alternative Schema I considered was

  • products
    • product 1
      • merchantName <- Field
      • productName <- Field
      • giftBuyer A-giftRecipient X: true
      • giftBuyer A-giftRecipient Y: true
    • product 2
      • merchantName <- Field
      • productName <- Field
      • giftBuyer B-giftRecipient X: true
      • giftBuyer B-giftRecipient Y: true

which would work better with the where() method but seems impossible for security rules.

Alternative schema suggestions are welcome.

These suggestions for feeds and chats don’t seem to fit my use case.

Firebase Firestore Structure for getting un-seen trending posts - Social

Firebase for getting unread posts

How to get all documents in a firestore subcollection based on the parent´s doc values?

Firestore: Get subcollection of document found with where

1 Answers1

1

I think I have a potential solution, but it requires cloud functions and denormalized data. Looking at this example of a follow/unfollow system, the products database can be structured as

  • products

    • product 1

      • merchantName <- Field

      • productName <- Field

      • giftBuyer A-giftRecipient X: true

      • giftBuyer A-giftRecipient Y: true

    • product 2

      • merchantName <- Field

      • productName <- Field

      • giftBuyer B-giftRecipient X: true

      • giftBuyer B-giftRecipient Y: true

where the permissions are set to all authorized users can read, but no one can write.

Then when gift buyers are searching for gift recipients, they will write to their own viewed products list

  • giftBuyers
    • giftBuyer A
      • giftRecipients
        • giftRecipient X
          • viewedProduts
            • product 1
            • product 2
        • giftRecipient Y
          • viewedProduts
            • product 1
            • product 2

Then I can use a triggered cloud function to write from the giftBuyer viewed products over to the products page.

The workflow is then

  1. Query database and ask for all products where there's no field "{$giftBuyer}-{$giftRecipient}".
  2. Write all viewed products to that giftBuyer.
  3. Every time we paginate, trigger the cloud function to write from the giftBuyer collection to the products collection.