Firebase Database query - only show posts from Friends [re: Social App] | C2C Community
Question

Firebase Database query - only show posts from Friends [re: Social App]

  • 15 March 2023
  • 3 replies
  • 92 views

Userlevel 3
Badge

Hi C2C community.
 

I’m building a social app with Firebase and had a basic question around database structuring and querying as it relates to posts and “friends” that I was hoping to get some guidance with.
 

Context

I have 2 collections: 

  • Users 

  • Posts


Custom Fields

  • Users have a field called: friends with a field type - List <Doc Reference (users)>

  • Posts have a field called: createdBy with a field type - Doc Reference (users)


Challenge
Display a feed of posts where the createdBy user is in my friends list?


My approach
Querying the posts collection only shows posts  where the createdBy value is in my friends list. 


Expected result
Return a list of posts only from users in my friends list


Actual result
A list of posts from all users.


Question
Is there a better way to query this type of request with Firebase? Would it be easier if friends were a subcollection of users?


Open to thoughts/suggestions.


Thanks in advance,


~Kevin


3 replies

Userlevel 2
Badge

In my view this is not a query challenge. It’s a data modelling challenge. 
you can achieve the same result without relying on the query’s where clause by using security rules. 
Just allow reads when the post’s created by has a reference in your user’s friends list. But instead of saving references in your friends list or users’ created by, save the document id.
Hope this helps. 

Userlevel 3

I think fb is not a good option for this kind of project.

 

My recommendation (after been trying building something similar) is just to use another db like postgress or MongoDB and handle the query server side.

Badge +1

Hi C2C community.
 

I’m building a social app with Firebase and had a basic question around database structuring and querying as it relates to posts and “friends” that I was hoping to get some guidance with.
 

Context

I have 2 collections: 

  • Users 

  • Posts


Custom Fields

  • Users have a field called: friends with a field type - List <Doc Reference (users)>

  • Posts have a field called: createdBy with a field type - Doc Reference (users)


Challenge
Display a feed of posts where the createdBy user is in my friends list?


My approach
Querying the posts collection only shows posts  where the createdBy value is in my friends list. 


Expected result
Return a list of posts only from users in my friends list


Actual result
A list of posts from all users.


Question
Is there a better way to query this type of request with Firebase? Would it be easier if friends were a subcollection of users?


Open to thoughts/suggestions.


Thanks in advance,


~Kevin

firebase with friends

Reply