I have a database consisting of reviews, follow, and users. Where users following other users is a many to many relationship modeled by the follow table. In total my schema looks as follows:
- follow (collection) - key: fid
- following (uid)
- follower (uid)
- review (collection) - key: rid
- title (string)
- author (uid)
- posted (timestamp)
- user (collection) - key: uid
- created (timestamp)
- email (string)
I want to run a query to get the T
most recent reviews where the user is following the author. In a SQL environment I would do this with two joins
and a where
clause.
Let us consider a user following n
people, where each person they're following has m
reviews. I was considering finding all reviews for all of the n
people one is following, then discarding all those older than T
, but recognize the number of reads will be n*m
. As we can easily expect n > 100
and m > 1000
, this is not a viable solution. I recognize there is probably no great way to do this in firestore. Any suggestions?
UPDATE: The top answer to a similar question is giving an nk
(where k
is an arbitrary limit) solution for a number of reads. It is also answering an easier question: "get the T
most recent reviews for each person one is following" not "get the T
most recent reviews of all people one is following." This answer, suggests keeping an updated copy of all followers in every review then doing a whereArrayContains
clause to find reviews one is following. But if user A
follows a user B
who has B_m
reviews, we will perform B_m
writes for each follow or unfollow. We will also be massively denormalizing our database, storing and updating the same information in thousands of locations.