Querying GCP Firestore with Python3
Querying GCP Firestore with Python3
Note: this assumes that you’ve set up your credentials and whatnot.
Collection name: collection1
Schema contents:
{'lname': 'Pasha', 'fname': 'Sasha'}
{'fname': 'Rasha', 'lname': 'Pasha'}
{'lname': 'Pasha', 'fname': 'Natasha', 'faves': '{"animal":"giraffe","color":"aqua","flavor":"mint"}'}
First, import the libs you need and define your collection:
from google.cloud import firestore
import json
db = firestore.Client()
col_ref = db.collection(u'collection1')
List all things in collection1:
# list everything in collection1
for x in col_ref.stream():
y = x.to_dict()
print(u'{} => {}'.format(x.id, y))
if "faves" in y:
print("faves:")
z = json.loads(y["faves"])
for fave in z:
print("{}: {}".format(fave, z[fave]))
Find only the people with last name of Pasha
allpashas = col_ref.where(u'lname', u'==', u'Pasha').stream()
for ap in allpashas:
p = ap.to_dict()
print(p["fname"], p["lname"])
Find only Natasha Pasha and display her name AND favorites:
natnat = col_ref.where(u'lname', u'==', u'Pasha').where(u'fname', u'==', u'Natasha').stream()
for n in natnat:
np = n.to_dict()
print(np["fname"],np["lname"])
if "faves" in np:
print(" {}'s faves:".format(np["fname"]))
ff = json.loads(np["faves"])
for f in ff:
print(" - {}: {}".format(f, ff[f]))