0

I've read this solution, but I still don't know how to count the number of documents in a collection with Python.

My code :

job = db.collection(u'netcollect_jobs').document('48c94805-1dab-4325-a927-f22f9c9b635d')
coll = job.collection('job_result_per_device')
coll_count = coll.count().get()
print(coll_count)
print(finished_device[0][0].value)

output :

[[<Aggregation alias=field_1, value=25003, readtime=2023-04-17 13:52:27.813282+00:00>]]
25003

Ok, I found the number of documents (25003), but it's weird to access it like I did (i.e [0][0].value). I feel like what I did is not 'official' (not recommanded). On documentation, I didn't find anything, neither in the github repo.

What's the best solution for achieve this ?

Rian
  • 85
  • 8
  • I'm not sure I understand the question. That `25003` in your output looks like it's the document count. Is that not what you expected? – Frank van Puffelen Apr 17 '23 at 14:55
  • @FrankvanPuffelen `25003` is my count, but it's weird to access to this data through the `[0][0].value`. Is it 'official' ? It will always works ? – Rian Apr 17 '23 at 15:41
  • From these docs: [1](https://cloud.google.com/python/docs/reference/firestore/latest/aggregation), [2](https://cloud.google.com/python/docs/reference/firestore/latest/google.cloud.firestore_v1.types.RunAggregationQueryResponse) and [3](https://cloud.google.com/python/docs/reference/firestore/latest/google.cloud.firestore_v1.types.AggregationResult) it seems correct. – Frank van Puffelen Apr 17 '23 at 16:24

0 Answers0