mawall
mawall

Reputation: 175

Query for documents containing subcollection

Is there a way to query for all documents containing a specific subcollection (or alternatively any subcollection at all) in a single query?

This answer: Is possible to check if a collection or sub collection exists? suggests using the document length. Is it possible to do this check within a single query?

Ideally something like this would be possible:

db.collection("stuff")
            .whereEqualTo("fu", true)
            .containing("subcollection");

Context: I am using a FirestoreRecyclerAdapter to populate a RecyclerView with this query. That's why I can't to the filtering locally. I am aware that introducing an indicator field in the document would solve the problem, but would ideally like to avoid this.

Upvotes: 0

Views: 166

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598688

I don't think such a query is possible.

The best I can think of is keeping a field in the parent document that flags when you add something to the subcollection.

db.collection("stuff")
  .whereEqualTo("fu", true)
  .whereEqualTo("somethingAddedToSubcollection", true);

Admittedly not ideal, since every write to the subcollection would now require a second write to the document itself, so I hope someone else knows a better solution.

Upvotes: 1

Related Questions