Reputation: 894
I'm building a 1-1 chat system in flutter which stores the chat data in a firestore subcollection for each pair of chatters. I would like to be able to show a list where the user can see his chat buddies and their latest messages (a la Whatsapp). I thought I could do something like:
Firestore.instance.collectionGroup('Chats').where('idFrom', isEqualTo: uid)
.limit(1)
.orderBy("timestamp", descending: true)
.snapshots()
But this just returns the very last message sent by the user. Could someone please tell me if this is doable at all, or maybe point out an alternative way of achieving what I'm trying to do? Right now the only other alternative I see is to remove the limit and then filter out the last messages client side, but that doesn't seem like a smart thing to do, cause the amount of messages in each subcollection can get very large.
Upvotes: 3
Views: 649
Reputation: 317412
What you're trying to do isn't possible with a single query. A collection group query considers all documents in all collections of the same name. It can't make special considerations for each different subcollection.
You will either have to query each subcollection individually, or duplicate the lastest message in a different subcollection that only ever contains just that one message.
Upvotes: 2