0

I have a commodity_price table within Firestore having the following structure -

collection / document / collection / document / field

Following is the type of the information contained -

commodity_price (the table) / city / commodity / date / price

Questions -

  1. How do I get a list of all the cities in the table?
  2. How do I get a list of cities having data for a specific date?

I tried using the following code but returns 0 records -

docs = db.collection('commodity_price').get() 
for doc in docs:
    print(doc.to_dict())
Karvy1
  • 959
  • 6
  • 14
  • 25

1 Answers1

0

For your first question, on how to get a list of all the cities in your commodity_price document I found this similar question which has an accepted answer which I think you can adapt to suit you use case. Something like:

docs = db.collection('commodity_price') 
results = docs.stream()
for result in results:
    city = u'{}'.format(doc.to_dict()['city'])
    print(city)


Addressing your second question, on how to get a list of cities filtering by a specific date. I think you could use a similar approach as before, but including a filter for the date that you want, for example:

date = "2022-01-01"

docs = db.collection('commodity_price') 
docs.where("date", "==", date)
results = docs.stream()
for result in results:
    city = u'{}'.format(doc.to_dict()['city'])
    print(city)

This last solution is based on this answer and this documentation

Lluís Muñoz
  • 409
  • 3
  • 11