Firestore - Python filter by value in a list

titipata

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.

Doug Stevenson

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Python - dynamic filter for list comprehension?

分類Dev

Python - Group By and filter for attribute value missing

分類Dev

Python modify value in list of dicts

分類Dev

Insert variable value into a list - python

分類Dev

Django: Check if value exists in object.filter list

分類Dev

Filter string and integer value from Dataframe object -python

分類Dev

Python Pandas - filter groups based on existence of value in group

分類Dev

Android FireStore retrieve as list

分類Dev

Python2 merge list of lists by value

分類Dev

How to make dict element value as list in Python

分類Dev

python bisect.insort(list、value)

分類Dev

Refreshing the value of a python list tag with jquery

分類Dev

Grouping python list of dictionaries and aggregation value data

分類Dev

Assign value to an item of the list in dictionary in Python

分類Dev

How to add a list as value in dictionary Python

分類Dev

Continous alphabetic list in python and getting every value of it

分類Dev

Python find the index of a multi value List?

分類Dev

Python find the index of a multi value List?

分類Dev

accessing second value in python list loop

分類Dev

Ionic Increment Firestore Value

分類Dev

get updated value with firestore

分類Dev

Python - List of unique dictionaries where each dictionary value is a list

分類Dev

Why does the value of a variable changes on appending to python list as a list?

分類Dev

Filter list - logic issue

分類Dev

Filter a list of strings by frequency

分類Dev

SwiftUI、List、.onDelete(perform :)、Firestore

分類Dev

How to show collection list in firestore?

分類Dev

Python - Create a List Starting at a Given Value and End at Given Length

分類Dev

Return specific value when list is out of range in Python

Related 関連記事

  1. 1

    Python - dynamic filter for list comprehension?

  2. 2

    Python - Group By and filter for attribute value missing

  3. 3

    Python modify value in list of dicts

  4. 4

    Insert variable value into a list - python

  5. 5

    Django: Check if value exists in object.filter list

  6. 6

    Filter string and integer value from Dataframe object -python

  7. 7

    Python Pandas - filter groups based on existence of value in group

  8. 8

    Android FireStore retrieve as list

  9. 9

    Python2 merge list of lists by value

  10. 10

    How to make dict element value as list in Python

  11. 11

    python bisect.insort(list、value)

  12. 12

    Refreshing the value of a python list tag with jquery

  13. 13

    Grouping python list of dictionaries and aggregation value data

  14. 14

    Assign value to an item of the list in dictionary in Python

  15. 15

    How to add a list as value in dictionary Python

  16. 16

    Continous alphabetic list in python and getting every value of it

  17. 17

    Python find the index of a multi value List?

  18. 18

    Python find the index of a multi value List?

  19. 19

    accessing second value in python list loop

  20. 20

    Ionic Increment Firestore Value

  21. 21

    get updated value with firestore

  22. 22

    Python - List of unique dictionaries where each dictionary value is a list

  23. 23

    Why does the value of a variable changes on appending to python list as a list?

  24. 24

    Filter list - logic issue

  25. 25

    Filter a list of strings by frequency

  26. 26

    SwiftUI、List、.onDelete(perform :)、Firestore

  27. 27

    How to show collection list in firestore?

  28. 28

    Python - Create a List Starting at a Given Value and End at Given Length

  29. 29

    Return specific value when list is out of range in Python

ホットタグ

アーカイブ