0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mawall
  • 171
  • 3
  • 10
  • Firestore doesn't support queries across collections. A single query may only use properties of documents in a single collection. Subcollections are are their own collections - they really have no relation to the higher level collection except by organization. – Doug Stevenson Jun 28 '18 at 16:39

1 Answers1

1

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807