1

I'm using python for trying to add a field to more than 750 documents in a firestore collection, but when trying to do this, only my first 55 documents get updated. I'm assuming this happens because of the firestore write limits but don't really get why.

COLLECTION STRUCTURE

enter image description here

Note: actividades is an array with a size around of 20 elements and each element has only 3 properties.

enter image description here

CODE

import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate('avancesAccountKey.json')
default_app = firebase_admin.initialize_app(cred)
db = firestore.client()

count = 0
avances = db.collection('avances').get()
for e in avances:  
    db.collection('avances').document(e.id).update({
        'vigente': False
    })
    count += 1

print(count) # count is 55 when trying to update, 757 otherwise

What is exactly happening and how I can solve this?

Javier Cárdenas
  • 3,935
  • 4
  • 25
  • 35
  • Which of the Firestore [documented limits](https://firebase.google.com/docs/firestore/quotas) do you think you're running into? Are you suggesting the count is different when you perform the update as opposed to leaving it commented out as you show now? – Doug Stevenson Sep 17 '18 at 04:45
  • It is diferent. The count is 757 without the comments. I don't really know why is happening, but i'm thinking that is because of one of the Firestore limits. – Javier Cárdenas Sep 17 '18 at 12:02

1 Answers1

1

You shouldn't mutate the collection as you iterate through it. Instead, fetch the document references and then iterate.

count = 0
documents = [snapshot.reference for snapshot in db.collection('avances').get()]
for document in documents:
    document.update({u'vigente': False})
    count += 1

Reference: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/6033

Update: It appears the Firestore python client does have a 20 second timeout for the generator returned from a query.

Juan Lara
  • 6,454
  • 1
  • 22
  • 31
  • why is bad mutating the collection while iterating? – Javier Cárdenas Sep 19 '18 at 22:28
  • It's a general good practice not to mutate the thing you're iterating over, because it can lead to unexpected results. Classic example is a [with a list](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list) but it applies here, too. – Juan Lara Sep 20 '18 at 23:07