[PYTHON] About Firestore timeout

background

Do you use Firebase, everyone? It's very convenient because it has all the functions you need to create a simple application. I also think that it is a very nice service, but there are specifications that may not be noticed if you glance at the official document, and I would like to share it this time.

problem

Look at the code below. This is the code to get all Documents in Collection.

docs = db.collection(u'cities').stream()

for doc in docs:
    print(u'{} => {}'.format(doc.id, doc.to_dict()))

Source: Official Document

So what happens if you change this code as below?

docs = db.collection(u'cities').stream()

for doc in docs:
    #nanka omoi syori
    time.sleep(100)
    print(u'{} => {}'.format(doc.id, doc.to_dict()))

If you try to execute it in this way, you can see that only the first one is output.

solution

If the content of the document to be acquired is small and the amount is not so large, for example, you can acquire all the documents once as follows.

docs = [doc.id for doc in db.collection(u'cities').stream()]

for doc_id in docs:
    #nanka omoi syori
    time.sleep(100)
    print(doc_id)

You may want to get a huge collection. In that case, please refer to the link below and use the query cursor. stack overflow question Official documentation for query cursors

Finally

I searched for Japanese materials such as Qiita articles that describe this matter, but there were no hits at all ...: cry: (as of February 2, 2020) I think this can be critical depending on the type of application, so I hope it helps those who are thinking about studying Firebase or who may use it in practice.

Recommended Posts

About Firestore timeout
About LangID
About CAGR
About virtiofs
About python-apt
About Permission
About sklearn.preprocessing.Imputer
About gunicorn
About locale
About permissions
About Opencv ②
About axis = 0, axis = 1
About import
About numpy
About pip
About Linux
About numpy.newaxis
About endian
About Linux
About import
About Opencv ①
About Linux
About Linux
About Linux ①
About cv2.imread
About _ and __
About wxPython