Home > Enterprise >  Query firebase docs by subcollection value
Query firebase docs by subcollection value

Time:06-12

I have a Firebase Firestore DB with this structure:

Users (collection) -> user (doc) -> Reports (subcollection) -> report (doc) -> isHandled (bool field).

In my Flutter app, I want to query Users collection, and get only the user docs in which any of their reports collection docs is not handled (isHandled field == false).

I've tried to use Firestore's collectionGroup, but it returns only 1 report doc, instead of many user docs:

await FirebaseFirestore.instance
        .collectionGroup('Reports')
        .where(
          'isHandled',
          isEqualTo: false,
        )
        .get();

Does Firebase Firestore support any query of a collection by its docs' subcollection values?

Thank you very much in advance!

CodePudding user response:

Does Firebase Firestore support any query of a collection by its docs' subcollection values?

No. When you query a collection or subcollection, it will only consider documents immediately within it. The data from a single collection's documents is managed by an "index" - queries only use that index to rapidly find data at scale.

Collection group queries are special in that they consider all collections with the same name, but they will still not consider nested subcollections.

You could perhaps combine a collection group query with further queries to get the "parent" user document from a matched report.

  • Related