0

I have this Firestore structure: Collection: Users
Documents: one for each users
2 sub collection: "in corso" and "finite"
Documents: many documents for each sub-collection

I need a way to loop every User in collection and update/delete the documents in the two sub collection of each user.

I tried this approach: https://pastebin.com/nqsGuMm6

Giving me the following exception because of Firestore Quotas Limit:

Exception from a finished function: Error: 4 DEADLINE_EXCEEDED: Deadline exceed. 

I'm trying to find a different approach. The only idea I came up with is calling another cloud function for each User and update his document with that other function. Not sure this is the best approach so I'm looking for other advices

Thank you

faccio
  • 652
  • 3
  • 16
  • faccio, the Stackoverflow question you mentioned in the comment is not visible now, let me know if the below recommendations were helpful. – Vaidehi Jamankar Nov 02 '22 at 07:25
  • Yes thank you, solved the issue with batch write – faccio Nov 02 '22 at 07:26
  • @VaidehiJamankar now I'm taking time to optimize the code https://stackoverflow.com/questions/74285115/how-to-unify-this-two-instructions – faccio Nov 02 '22 at 07:27

1 Answers1

0

That error can be caused by writing too much data to firestore at once. The error also appears sometimes on poor internet connections or bugs in the code,do check your internet connections once. Also to do this efficiently, you must use a batched write. This allows you to perform multiple updates and delete operations simultaneously.You can batch writes across multiple documents, and all operations in the batch complete atomically.But if you have more than 500 documents to update, you will still have to do some iteration to update them all.
You can find more about the Usage Quota and Limits here. Here are a few examples of similar implementations of multiple delete and batched deletes here.

Vaidehi Jamankar
  • 1,232
  • 1
  • 2
  • 10