I'm using Firestore as my NoSQL database and have struggled to try to work out the best structure to allow querying to conditionally send batched push notifications.
Previously I had this type of NoSQL structure (collection/doc/subcollection/doc):
users/ <-- root collection
[userId]
{ push token }
items/ <-- sub collection
[itemId]
{ item data }
I would store the user push token at the users/[userId]
level but the actual data for the user in the sub-collection.
The issue I found was querying across all users depending on the data in their sub-collection.
My next solution was to move the sub-collections to their own root collection, and each document has a reference to the users/[userId]
that the data belonged to.
users/
[userId]
{ push token }
items/
[itemId]
{ item data, [userId] }
This would allow me to run the following logic in a cloud function:
- Store push tokens in a
users
root collection - Fetch all user docs that have a stored push token
- Loop over the above user ids, and conditionally fetch item data that share a user id
Is this a good structure for working out which users you need to send a push notification to?