Home > Enterprise >  Firestore - Update collection field when all subdocuments have a specific field value
Firestore - Update collection field when all subdocuments have a specific field value

Time:11-15

Is there a way to watch/check all sub documents within a collection in firestore? I have a collection that has many subdocuments. Each subdocument has a field called status. I'm looking for a way to "watch/check" all subdocuments and when each document's status field is "success" the collection can be marked as completed and the end user notified the task is completed.

Thanks!

CodePudding user response:

You'll have to run a query on the documents in the subcollection to determine if there are any documents not "success" yet.

Alternatively, add a field to your parent document that counts the number of child documents with a "not success" status, update this every time you write/update/delete a status in a child document, and then update the parent document once the count reaches 0.

  • Related