0

My Firebase structure is like follows:

Posts
  - post1
     - timestamp
     - groupID 
     - text 
  - post2
     - timestamp
     - groupID 
     - text 

Users 
  - user1 
     - groups
        - group1
        - group2 
     - some other data ...
  - user2
     - groups
        - group1 
        - group3
     - some other data ... 

I want to implement a "news feed" (kind of like Facebook has), in which all posts from all groups a specific user is a member of, are shown. So, in my example I'll first read the groups the user is a member of (e.g. "group1" and "group3" for "user2". These keys correspond to "groupID" in the "Posts" node. I want to get all posts, where groupID == group1 OR groupID == group2 sorted by the timestamp. How am I to do this? I tried ordering the posts by groupID via the "equal"-query but this won't give me the possibility to sort by timestamp as Firebase does not support multiple query-ordering.

picciano
  • 22,341
  • 9
  • 69
  • 82
pmax1
  • 226
  • 1
  • 4
  • 17
  • Possible duplicate of [Query based on multiple where clauses in Firebase](https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase) – André Kool Apr 12 '18 at 20:15
  • When the user logs in, why don't you just iterate over the groups they belong to and add observers to those groups. If you change your structure to *groups/posts* then it can be queried by timestamp so say, only items in the last 72 hours are shown. – Jay Apr 13 '18 at 18:08

1 Answers1

0

For complex queries you may have to use firestore as Firebase doesn't support any complex queries just simple ordering and stuff.

Firebase suggest to solve these kind of problems by flattening the data and allow us to replicate some of the data.

For your case you can change post id to groupId instead so you can easily read posts of a specific groupId.

If you need post id to be there then you can create new node like groupPosts such as groupPosts-> GroupID-> PostId1 PostId2 GroupID2-> PostId3 ...........

If you need some data like createdAt, post title you can keep here but no need of all data.

Guru
  • 286
  • 3
  • 6