franco_b
franco_b

Reputation: 878

Firestore: List subcollections of a document using python

Is it possible list subcollections of a document using python? It seems that google documentation is discordant

Here they say that get collections method is not available in the Python client library:

https://firebase.google.com/docs/firestore/query-data/get-data#python_6

Here they say that class collections() list subcollections:

https://googleapis.dev/python/firestore/latest/document.html

So I try something like:

collnameref = db.collection(collname)
docs = collnameref.stream()
for doc in docs:
    print (doc.collections())

but it doesn't work.

Upvotes: 3

Views: 4371

Answers (1)

Juan Lara
Juan Lara

Reputation: 6854

You're right. This is missing from the documentation:

collnameref = db.collection(collname)
docs = collnameref.stream()
for doc in docs:
    # List subcollections in each doc
    for collection_ref in doc.reference.collections():
        print(collection_ref.parent.path + collection_ref.id)

Upvotes: 13

Related Questions