1

Firestore structure I have a parent collection 'chats' on firestore with different ids which I am sending from the app. Each id also have a 'messages' collection. The major thing I want to achieve is iterating through the documents of the parent (i.e 'chats') collection and checking if the document contains an id (e.g 31) then do something with the message collection of that document.

await FirebaseFirestore.instance.collection('chats').get().then((value){
      print('value Size: ${value.size}'); //value.size here is returning 0
      for (var element in value.docs) {
        if (element.id.contains('31')) {
          //Do something with the messages collection.
FirebaseFirestore.instance.collection('chats/${element.id}/messages').get().then((value){
      for (var message in value.docs) {
        // Do something with the message data
      }
    });
        }
      }
    });

The value.size is returning 0. However, I don't have issues when I want to perform the same operation on the 'messages' collection.

Please, how can I acheive this??? Thank you in advance.

developerTobi
  • 41
  • 1
  • 8
  • 1
    Not really sure but why are you using "await" and "then" in the same request? – Shahzad Umar Baig Jun 11 '22 at 12:28
  • @ShahzadUmarBaig. I want to show a circular progress bar while it performs the operation. Is that why I am not able to acheive what I want to achieve? – developerTobi Jun 11 '22 at 13:28
  • That doesn't seems to be the issue here, in that case the value.size should not be 0. Have you tried logging value.docs.length? – Shahzad Umar Baig Jun 11 '22 at 13:39
  • It's also returning 0 – developerTobi Jun 11 '22 at 14:15
  • Your code is querying the top-level collection "chats", but your screenshot is showing documents from a deeply nested subcollection. Queries don't fetch nested subcollections, just documents immediately within the named collection. The result of your query is telling you that there are no documents immediately within the "chats" collection. – Doug Stevenson Jun 11 '22 at 14:33
  • Thanks for your response. The screenshot is just to show the structure of the firestore database. I understand what you just explained but how can I achieve what I want to achieve? Or is it not achievable? @DougStevenson – developerTobi Jun 11 '22 at 17:55
  • @DougStevenson. I have edited the code in the question to show the exact thing I'm trying to do. Thank you – developerTobi Jun 11 '22 at 18:11

1 Answers1

0

Take a close look at the documents listed under "chats". They are all greyed out and in italics. That means there are no actual documents there. What you have are subcollections nested under the document IDs that don't exist.

If you have no documents in a collection like this, then your query on "chats" will not find anything - your code is working exactly as I would expect.

If you want to be able to list those IDs where there are no documents, you won't be able to do that from your client web app. You can do this in your backend code, though. Please read: How to find Firestore collections which don't have parent documents?

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441