0

I have a firestore like this

ppl (collection)
   1 (document)
    school(collection)
        jhdsfjhdsgfjh (random generated document)
               jobname(collection field)
                  teacher:1 (collection field1)
                  advisor:2 (collection field2)

I want to extract teacher and advisor in a JSON file but I am not able to

my code so far:

 doc_ref = db.collection(u'ppl').document('1')
 doc = doc_ref.get
 docuout =  doc_ref.collection('school').document().collection().stream()

 document_output=docuout.to_dict()

it's returning an empty document_output. Any idea how to fix it ?

JM17
  • 149
  • 1
  • 11

1 Answers1

0

Every time you call document() without parameters, it generates a reference to a new/non-existing document. So you're trying to stream a subcollection of a non-existing document, which explains why you don't get any results.

If you want to get the subcollection for a specific document, specify the ID for that document in the call to document("id here") as shown in the docs on getting a document. If you don't know the ID of the document, you will need to know something else that uniquely identifies the document and can then use a query to find the document.

If you want to get a random document from the school collection, have a look at Dan's excellent answer here: Firestore: How to get random documents in a collection

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807