Firestore Python: Get Multiple Documents

April 25, 2019
doc_refs = [db.collection('test').document('xxx'), db.collection('test').document('Gyxnw0vD4dTuCYM8gOTs')]
docs = db.get_all(doc_refs)
for doc in docs:
    if doc.exists:
        content = doc.get('created')
        log.info(f"id=${doc.id}, content=${content}")
    else:
        log.info("not found")

NOTE: I believe this feature is only available for server client, not available for Android/iOS/Web

NOTE: Client.get_all

Documents returned by this method are not guaranteed to be returned in the same order that they are given in references.

If multiple references refer to the same document, the server will only return one result.

Though the documentation mentioned:

Yields: .DocumentSnapshot – The next document snapshot that fulfills the query, or None if the document does not exist.

I tested with firebase-admin == 2.16.0 which utilize google-cloud-firestore >= 0.31.0

  • the return documents iterator is never None, even when doc_refs is empty.
  • DocumentSnapshot is never None. If the document doesn’t exist, check for DocumentSnapshot.exists.
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.