1

I'm using Cloud Firestore on my mobile app. I want to get contents from users I follow. Right now those:

enter image description here

So I'm trying the following query to get contents from people I follow.

ref = FirebaseFirestore.getInstance().collection("topics");
ref.whereEqualTo("username","user1");
ref.whereEqualTo("username","user2");

But when I use this query it only shows posts from first user (user1).

Also what If I follow 10K users. What's the best way to get latest posts from them?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Bucky
  • 1,116
  • 2
  • 18
  • 34

3 Answers3

4

That kind of query you're trying to perform isn't supported by Firestore. You're assuming that adding more calls to whereEqualTo() will each apply like a logical OR, and that any document that matches any of the conditions will be returned. In Firestore, adding more conditions is a logical AND, meaning all of the conditions must be true for a document to match.

You will have to restructure your database in order to support the type of query you want to perform. This is very common for noSQL type databases. There is not a single structure that supports all kinds of queries. In your case, you will need to record per-user which other users they follow, and query that to get what you want. You may need to perform multiple queries to get everything required for your case.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Can you give any examples for that? I mean Firebase works great in all other parts of this app. But what I want to do is possible using Cloud Functions. So what I can do is copy a new post to all its author's follower section. So that their follower can get all following posts using a direct query. But what if the user has 100k followers. Should I run the loop for 100k times ? – Bucky Jan 23 '18 at 17:00
  • 1
    You'll either spend more code/time duplicating the data upon writes, or you'll spend more code/time reading the data. There is no other option. Well... unless you want to get a range of users, in which case you can `startAt("user01").endAt("user10")`. – Frank van Puffelen Jan 23 '18 at 17:31
2

There are two ways to deal with this thing. The first is by using cloud functions for Firebase to listen for any document created by the user and then calls a method which will send a copy of the document created to every user who is following. The second is that one Doug Stevenson mentioned, basically you have to create a collection or a document containing all the users you follow then from that you can call for each one to get your data.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
Odai A. Ali
  • 845
  • 3
  • 8
  • 23
  • 1
    What I am currently doing is the first process you mentioned. But will I get any problems, If the followers count is more than 100k+? – Bucky Jan 23 '18 at 17:47
  • 1
    Cloud functions is the right choice for your case ,it scales as your users node grows .Don't worry cloud functions can handle it easily. – Odai A. Ali Jan 23 '18 at 18:14
  • @Robotec as you mentioned on first process what if a user got new follower doe's Firebase function will auto add post to that new follower ? – Joel Jerushan May 12 '20 at 04:57
  • Indeed it will, Suppose that a new follower added his/her user ID to your followers' IDs list , what the firebase function (triggered once you write a post) would do is read that list of IDs and send the post to each one in it. You need to use batch writes to handle the process of sending posts to followers. – Odai A. Ali May 12 '20 at 09:28
0

I have recently found a solution for this type of problem. You can check it out here at this link : https://stackoverflow.com/a/70861741/11351916

Nader Khaled
  • 145
  • 2
  • 8