0

In a chat app, I have a user object which is subscribed to channels (e.g. each user contains the channel ID list in its document):

User
   Channels
      ID 1
      ID 7

I want to be able to subscribe to channels based on the user channels list (pseudo-code):

channelsCollection.documentsWhereIn("id",[1,7]).addSnapshotListener...

I can't find any way of actually achieving this, so the only solution I've been able to come up with is maintaining the subscribed users in the channel list

Channel
   Users
      UserId1:True
      UserId7:True
      ...

And then subscribing with

channelsCollection.whereEqualTo("users.id",myId).addSnapshotListener...

The downside is that I feel that this is a bit fragile to maintain on the channel document, in the end it's a javascript object that could easily be overridden by some mistake. I'm looking for a more granular solution.

I was considering subcollections, but saw here that I can't query based on them yet.

I think this hasn't been answered yet because:

What's the best way to get data from collections nested in documents in Firestore? - doesn't provide a solution to extracting only user-related channels/chats

Firestore query subcollections - Mentions that I can't query based on subcollections

Shahar
  • 478
  • 5
  • 17
  • There is currently no API to get multiple individual documents in a single call by specifying their IDs. See https://stackoverflow.com/questions/46721517/google-firestore-how-to-get-document-by-multiple-ids-in-one-round-trip – Frank van Puffelen Nov 09 '17 at 08:25
  • if you store the reference of the channel in a user you could do two get requests. One get to get the list of channel references. Then you could do a `firestore().getAll(refList)` to get all the documents with those references – NeXtMaN_786 Feb 17 '18 at 21:36

0 Answers0