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
- giftRecipient X
- giftRecipients <-subcollection
- giftBuyer B
- giftRecipients <-subcollection
- giftRecipient X
- giftRecipient Y
- giftRecipients <-subcollection
- giftBuyer A
- product 2
- …
- product 1
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
- product 1
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?