Firestore collection group dont working with where clauses

…衆ロ難τιáo~ 提交于 2021-01-24 08:06:22

问题


I'm using FlutterFire to develop an Android app using Firebase server-less service.

But, when I try to use collection group to query some data, the query return empty result when I use where clause.

In my database I have one collection called 'users', Inside every document in 'users' exist one subcollection called 'chats', like that:

users/{userDocument}/chats/{chat}

One chat is a conversation between two users, one user has the document chat inside his own document, the other user is referenced by the documentId of document chat, the documentId is stored in uid field using a cloud function onCreated. DocumentChat example

So, when I try to get all chats using collectionGroup, I use:

Firestore.instance
        .collectionGroup('chats')
        .where('uid', isEqualTo: userId)
        .snapshots(includeMetadataChanges: true);

In this case, 'userId' has the Id of the User 1, and 'uid' has the Id of user 2. In app, userId reference 'uid', and I guarantee the variable userId has the same value of field 'uid'.

I created this code to print userId value

static Stream<QuerySnapshot> getStreamChats(userId) {
       print('querying userId: $userId');
       Stream<QuerySnapshot> _stream = Firestore.instance
        .collectionGroup('chats')
        .where('uid', isEqualTo: userId)
        .snapshots(includeMetadataChanges: true);
      _stream.listen((_q) => print(_q.documents));
      return _stream;
  }

And, when it runs, output this:

I/flutter ( 4044): querying userId: YNANBZqQdNg5wYkzXRTNkPWwCHC3

I/flutter ( 4044): []

When I run this query without the where clause, the query returns all documents inside chats collections.

I don't checked any rules yet, so it's all allowed to read/write.

Indexes are created

来源:https://stackoverflow.com/questions/59524640/firestore-collection-group-dont-working-with-where-clauses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!