0
  • I have a social media app.
  • All users can like posts.
  • I wanna integrate a function to see all the posts that a user has liked.

The database structure is something like this:

I already tryed adding a list called likes inside each post with each name of the users who have liked the post

post example

Then query the data with this:

Query query = myRef.orderByChild("likes/" + uid).equalTo(true);
Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • Your data structure makes it easy to to find the users that liked a specific post. It does not however make it easy to find the posts that a specific user liked. To allow that you'll need to create an additional data structure where you do the exact inverse: `likedPosts/$uid` and then `$postId: true` for each post. Also see my explanation on why this is needed here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value and https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase – Frank van Puffelen Sep 14 '21 at 02:30

0 Answers0