BluE_MoOn
BluE_MoOn

Reputation: 103

How to Query for documents in a sub collection in Firestore?

I know there is some limitation on how to query documents in a subcollection from the root collection, but when you have a reference to the sub collection it shouldnt be a problem, should it (???)

The structure looks like this:

SensorName (Collection) -> MyDocument (Document) -> SensorHistory (SubCollection)

doc_ref = db.collection('sensorData').document('Engineroom').collection('History').order_by("timestamp").where(u"SensorType", u"==", 'temp')
result = doc_ref.stream().to_dict()
print(result)

This is the error i am getting

result = doc_ref.stream().to_dict() AttributeError: 'generator' object has no attribute 'to_dict'

Do I really need to put my History collection in the root to able to query it ?

Or is it me that have made a mistake in my code ? :)

Thanks for any help :)

Upvotes: 0

Views: 482

Answers (1)

Juan Lara
Juan Lara

Reputation: 6864

The stream() method returns a generator of document snapshots. You need to get the doc snapshots out of the generator before you call to_dict:

result = doc_ref.stream()

for doc in result:
    print(doc.to_dict())

Upvotes: 1

Related Questions