Reputation: 401
I have tried multiple ways to use .where() with firestore, in the end i just by passed .where() and used .get() then if/else statements to check data. With Javascript it is fairly simple. Python the docs are not easy to understand.
doc_reffer = db.collection(u'test')
emails = doc_reffer.where(u'adminEmail', u'==', '[email protected]') \
.get()
if(emails == True):
return "is true"
else:
return "false"
Neither of these work properly, i get a reponse but then cannot check anything on that object. What methods can be used on .where() and how is it used properly?
doc_reffer = db.collection(u'test')
emails = doc_reffer.where(u'adminEmail', u'==', '[email protected]')
return str(emails)
Upvotes: 0
Views: 1575
Reputation: 11
The other answers seem correct. You mentioned that the docs are not easy to understand with python client libraries. This is one of the problems I am trying to solve with flask-boiler. With flask-boiler, you will be able to query your objects like this:
emails = [x for x in Test.where(admin_email='[email protected]')]
for email in emails:
print(u'{} => {}'.format(email.doc_id, email.to_dict()))
Thanks.
Upvotes: 0
Reputation: 401
Using Juan Laras answer i was able to successfully get all documents within a collection where this admin email would exist. Which is what i wanted to achieve. There's pretty much no documentation (simple docs) that i could find for this.
doc_reffer = db.collection(u'test')
responseUserEmail = '[email protected]'
query = doc_reffer.where(u'adminEmail', u'==', responseUserEmail)
docs = [snapshot.id for snapshot in query.stream()]
if responseUserEmail in docs:
return f'found {responseUserEmail}'
else:
return f'did not find {responseUserEmail}'
Upvotes: 0
Reputation: 6854
Use the .stream()
method to execute the query:
doc_reffer = db.collection(u'test')
emails = doc_reffer.where(u'adminEmail', u'==', '[email protected]').stream()
for doc in emails:
print(u'{} => {}'.format(doc.id, doc.to_dict()))
Note that stream()
returns an iterator and won't fetch the documents until you start iterating through the results.
You can fetch all the docs, and put them into a dict like this:
doc_reffer = db.collection(u'test').where(u'adminEmail', u'==', '[email protected]')
emails = [snapshot for snapshot in doc_reffer.stream()]
Upvotes: 2