简体   繁体   中英

How to get all documents belong to specific subcollection using Flutter and Firestore

Let's say, I have this kind of architecture in Firestore;

   - Users (collection)
     - User A (document)
       - Notes (collection)
         - Note A (document)
         - Note B (document)
         - Note C (document)
     - User B (document)
       - Notes (collection)
         - Note D (document)
         - Note E (document)
         - Note F (document)

And I want to get all notes of all users' (Note A,B,C,D,E,F for this example)

How can I achieve that in Flutter?

You need to use a Collection Group query as follows:

QuerySnapshot snapshot = await Firestore.instance.collectionGroup('Notes').get();

As explained in the doc :

A collection group consists of all collections with the same ID. By default, queries retrieve results from a single collection in your database. Use a collection group query to retrieve documents from a collection group instead of from a single collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM