1

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.

thedeg123
  • 408
  • 2
  • 5
  • 11
  • Hi @thedeg123 indeed, Firestore is not meant for this type of queries. It would be better for you to read the data into arrays/variables and do the search within this variables. I, however, have found a similar question to yours, where there are some interesting information. Could you please check [here](https://stackoverflow.com/questions/46979375/firestore-how-to-structure-a-feed-and-follow-system) to verify if this helps you? – gso_gabriel Apr 30 '20 at 12:19
  • @gso_gabriel only somewhat, the top answer (https://stackoverflow.com/a/52153332/7834942) is essentially a hierarchical interpretation of what I already have. Additionally, it gives the same query that I was considering above! With the adjustment of imposing an arbitrary limit of k reviews per user, implying nk reads instead of nm. The next answer is better (https://stackoverflow.com/a/54741889/7834942) , but if user A follows a user B who has B_m reviews, we will perform B_m writes for each follow. – thedeg123 Apr 30 '20 at 15:32

2 Answers2

1

Your data seems highly relational so the best option for you is to switch to a relational database. The only way to get this to work in firestore is to either completely denormalize your data or chain a ton of queries together to get all of the data you need, neither is ideal in my opinion.

Long Nguyen
  • 426
  • 3
  • 8
1

there's a way that worked for me which is creating a link between indexes, how is that ? going to firestore => indexes and add index with this you can link the fields you want to query on and then you will be able to do this query in your code

neotrix
  • 47
  • 5