Here, I have data stored in Firestore and I have a key timeslots
that store JSON list that I would like to filter by string value (e.g. 3 AM (EST), 4 AM (EST)) directly from Firebase. Normally, I can do the following in Python:
from google.cloud import firestore
db = firestore.Client()
ref = db.collection('submissions')
submissions = []
for idx, doc in enumerate(ref.stream()):
submission = doc.to_dict()
submission['id'] = doc.id
submissions.append(submission)
where submissions
is a list that look something like follows:
[{'submission_date': DatetimeWithNanoseconds(2020, 7, 10, 20, 58, 9, 330598, tzinfo=<UTC>),
'title': 'Testing submission',
'authors': ['Mr. A', 'Mr. B'],
'timeslots': ['3 AM (EST)', '4 AM (EST)'],
'abstract': 'No abstract here',
'id': '12345'},
{'submission_date': DatetimeWithNanoseconds(2020, 7, 10, 20, 58, 9, 330598, tzinfo=<UTC>),
'title': 'Testing submission 2',
'authors': ['Mr. C', 'Mr. D'],
'timeslots': ['4 AM (EST)', '5 AM (EST)'],
'abstract': 'No abstract here too',
'id': '123456'},
...
]
Generally, I do the following to filter time == 3 AM (EST)
in this case:
submissions_filtered = [
submission for submission in submissions
if '3 AM (EST)' in submission.get('timeslots', '')
]
I'm not sure if I can do it directly from Firestore stream
(or something else). If someone knows, that would be great.
If you want to filter documents based on some string in an array type field, you can use an "array-contains" query as shown in the documentation.
ref = db.collection('submissions').where('timeslots', 'array_contains', '3 AM (EST)')
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加