0

I have Project docs that have Items subcollections. Now I want to fetch all projects that have at least one item in their subcollection. Is that possible?

Something like:

getDocs(collection(getDB(), "Projects"), where("Items.size",">",0);

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Obiwahn
  • 2,677
  • 2
  • 26
  • 36

1 Answers1

2

This is not possible with Firestore. What you should do instead is maintain a separate integer field in the document that tracks the size of the Items subcollection so that you can query it separately. You will need to make sure that any changes to the Items subcollection result in changes to that new field that tracks its size.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • https://stackoverflow.com/questions/60458687/filtering-documents-basing-on-the-documents-in-their-subcollections/60458769#60458769 might also be relevant – Frank van Puffelen May 19 '22 at 12:54