Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Firestore: Can't iterate through all query results #6033

Closed
jlara310 opened this issue Sep 19, 2018 · 3 comments
Closed

Cloud Firestore: Can't iterate through all query results #6033

jlara310 opened this issue Sep 19, 2018 · 3 comments
Assignees
Labels
api: firestore Issues related to the Firestore API. type: question Request for information or clarification. Not an issue.

Comments

@jlara310
Copy link
Contributor

google-cloud-firestore
Version: 0.29.0
Linux, Python 2.7.13

When you try to iterate over a query result, the generator times out after a while and does not return an error. Query result generator will silently miss results in queries with many documents.

from google.cloud import firestore
db = firestore.Client()
coll_ref = db.collection(u'users')

count = 0
mycoll = db.collection(u'users').get()

for e in mycoll:
    # There are 1000 docs in this collection. Only about 283 get updated. No error message  
    db.collection(u'users').document(e.id).update({
        u'myfield': False
    })
    count += 1
    # time.sleep(.5) the longer this wait is, the fewer docs get updated

print(count)

Issue brought up in this SO post.

@tseaver tseaver added type: question Request for information or clarification. Not an issue. api: firestore Issues related to the Firestore API. labels Sep 19, 2018
@tseaver
Copy link
Contributor

tseaver commented Sep 19, 2018

Your example mutates the collection while iterating it, which isn't likely to be safe even for an in-process datastructure like list or dict. This example fetches the collection snapshots first, converting them to document references, and then iterates them, performing the updates:

>>> documents = [snapshot.reference for snapshot in mycoll.get()]
>>> count = 0
>>> for document in documents:
...     document.update({u'myfield': False})
...     count += 1
...     time.sleep(0.01)
>>> count
1000

@tseaver tseaver closed this as completed Sep 19, 2018
@jlara310
Copy link
Contributor Author

Thank you for the prompt reply!

@tseaver
Copy link
Contributor

tseaver commented Sep 21, 2018

@jlara310 #6043 is a request to document that Query.get / Collection.get have a time limit on iteration, which explains why making longer calls to time.sleep reduced the number of rows in your example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants