1818
1919from google .api_core .client_options import ClientOptions
2020from google .cloud import firestore
21+ from google .cloud .firestore_v1 .base_query import FieldFilter
2122
2223
2324def quickstart_new_instance ():
@@ -238,7 +239,11 @@ def get_simple_query():
238239 db = firestore .Client ()
239240 # [START firestore_data_query]
240241 # Note: Use of CollectionRef stream() is prefered to get()
241- docs = db .collection ("cities" ).where ("capital" , "==" , True ).stream ()
242+ docs = (
243+ db .collection ("cities" )
244+ .where (filter = FieldFilter ("capital" , "==" , True ))
245+ .stream ()
246+ )
242247
243248 for doc in docs :
244249 print (f"{ doc .id } => { doc .to_dict ()} " )
@@ -250,7 +255,9 @@ def array_contains_filter():
250255 # [START firestore_query_filter_array_contains]
251256 cities_ref = db .collection ("cities" )
252257
253- query = cities_ref .where ("regions" , "array_contains" , "west_coast" )
258+ query = cities_ref .where (
259+ filter = FieldFilter ("regions" , "array_contains" , "west_coast" )
260+ )
254261 # [END firestore_query_filter_array_contains]
255262 docs = query .stream ()
256263 for doc in docs :
@@ -451,7 +458,7 @@ def compound_query_example():
451458 cities_ref = db .collection ("cities" )
452459
453460 # Create a query against the collection
454- query_ref = cities_ref .where ("state" , "==" , "CA" )
461+ query_ref = cities_ref .where (filter = FieldFilter ( "state" , "==" , "CA" ) )
455462 # [END firestore_query_filter_eq_string]
456463
457464 return query_ref
@@ -462,7 +469,7 @@ def compound_query_simple():
462469 # [START firestore_query_filter_eq_boolean]
463470 cities_ref = db .collection ("cities" )
464471
465- query = cities_ref .where ("capital" , "==" , True )
472+ query = cities_ref .where (filter = FieldFilter ( "capital" , "==" , True ) )
466473 # [END firestore_query_filter_eq_boolean]
467474
468475 print (query )
@@ -473,9 +480,9 @@ def compound_query_single_clause():
473480 # [START firestore_query_filter_single_examples]
474481 cities_ref = db .collection ("cities" )
475482
476- cities_ref .where ("state" , "==" , "CA" )
477- cities_ref .where ("population" , "<" , 1000000 )
478- cities_ref .where ("name" , ">=" , "San Francisco" )
483+ cities_ref .where (filter = FieldFilter ( "state" , "==" , "CA" ) )
484+ cities_ref .where (filter = FieldFilter ( "population" , "<" , 1000000 ) )
485+ cities_ref .where (filter = FieldFilter ( "name" , ">=" , "San Francisco" ) )
479486 # [END firestore_query_filter_single_examples]
480487
481488
@@ -484,10 +491,12 @@ def compound_query_valid_multi_clause():
484491 # [START firestore_query_filter_compound_multi_eq]
485492 cities_ref = db .collection ("cities" )
486493
487- denver_query = cities_ref .where ("state" , "==" , "CO" ).where ("name" , "==" , "Denver" )
488- large_us_cities_query = cities_ref .where ("state" , "==" , "CA" ).where (
489- "population" , ">" , 1000000
494+ denver_query = cities_ref .where (filter = FieldFilter ("state" , "==" , "CO" )).where (
495+ filter = FieldFilter ("name" , "==" , "Denver" )
490496 )
497+ large_us_cities_query = cities_ref .where (
498+ filter = FieldFilter ("state" , "==" , "CA" )
499+ ).where (filter = FieldFilter ("population" , ">" , 1000000 ))
491500 # [END firestore_query_filter_compound_multi_eq]
492501 print (denver_query )
493502 print (large_us_cities_query )
@@ -497,15 +506,19 @@ def compound_query_valid_single_field():
497506 db = firestore .Client ()
498507 # [START firestore_query_filter_range_valid]
499508 cities_ref = db .collection ("cities" )
500- cities_ref .where ("state" , ">=" , "CA" ).where ("state" , "<=" , "IN" )
509+ cities_ref .where (filter = FieldFilter ("state" , ">=" , "CA" )).where (
510+ filter = FieldFilter ("state" , "<=" , "IN" )
511+ )
501512 # [END firestore_query_filter_range_valid]
502513
503514
504515def compound_query_invalid_multi_field ():
505516 db = firestore .Client ()
506517 # [START firestore_query_filter_range_invalid]
507518 cities_ref = db .collection ("cities" )
508- cities_ref .where ("state" , ">=" , "CA" ).where ("population" , ">=" , 1000000 )
519+ cities_ref .where (filter = FieldFilter ("state" , ">=" , "CA" )).where (
520+ filter = FieldFilter ("population" , ">=" , 1000000 )
521+ )
509522 # [END firestore_query_filter_range_invalid]
510523
511524
@@ -540,7 +553,11 @@ def order_where_limit():
540553 db = firestore .Client ()
541554 # [START firestore_query_order_limit_field_valid]
542555 cities_ref = db .collection ("cities" )
543- query = cities_ref .where ("population" , ">" , 2500000 ).order_by ("population" ).limit (2 )
556+ query = (
557+ cities_ref .where (filter = FieldFilter ("population" , ">" , 2500000 ))
558+ .order_by ("population" )
559+ .limit (2 )
560+ )
544561 results = query .stream ()
545562 # [END firestore_query_order_limit_field_valid]
546563 print (results )
@@ -560,7 +577,9 @@ def order_where_valid():
560577 db = firestore .Client ()
561578 # [START firestore_query_order_with_filter]
562579 cities_ref = db .collection ("cities" )
563- query = cities_ref .where ("population" , ">" , 2500000 ).order_by ("population" )
580+ query = cities_ref .where (filter = FieldFilter ("population" , ">" , 2500000 )).order_by (
581+ "population"
582+ )
564583 results = query .stream ()
565584 # [END firestore_query_order_with_filter]
566585 print (results )
@@ -570,7 +589,9 @@ def order_where_invalid():
570589 db = firestore .Client ()
571590 # [START firestore_query_order_field_invalid]
572591 cities_ref = db .collection ("cities" )
573- query = cities_ref .where ("population" , ">" , 2500000 ).order_by ("country" )
592+ query = cities_ref .where (filter = FieldFilter ("population" , ">" , 2500000 )).order_by (
593+ "country"
594+ )
574595 results = query .stream ()
575596 # [END firestore_query_order_field_invalid]
576597 print (results )
@@ -689,7 +710,7 @@ def on_snapshot(col_snapshot, changes, read_time):
689710 print (f"{ doc .id } " )
690711 callback_done .set ()
691712
692- col_query = db .collection ("cities" ).where ("state" , "==" , "CA" )
713+ col_query = db .collection ("cities" ).where (filter = FieldFilter ( "state" , "==" , "CA" ) )
693714
694715 # Watch the collection query
695716 query_watch = col_query .on_snapshot (on_snapshot )
@@ -729,7 +750,7 @@ def on_snapshot(col_snapshot, changes, read_time):
729750 print (f"Removed city: { change .document .id } " )
730751 delete_done .set ()
731752
732- col_query = db .collection ("cities" ).where ("state" , "==" , "CA" )
753+ col_query = db .collection ("cities" ).where (filter = FieldFilter ( "state" , "==" , "CA" ) )
733754
734755 # Watch the collection query
735756 query_watch = col_query .on_snapshot (on_snapshot )
@@ -853,7 +874,9 @@ def collection_group_query(db):
853874 # [END firestore_query_collection_group_dataset]
854875
855876 # [START firestore_query_collection_group_filter_eq]
856- museums = db .collection_group ("landmarks" ).where ("type" , "==" , "museum" )
877+ museums = db .collection_group ("landmarks" ).where (
878+ filter = FieldFilter ("type" , "==" , "museum" )
879+ )
857880 docs = museums .stream ()
858881 for doc in docs :
859882 print (f"{ doc .id } => { doc .to_dict ()} " )
@@ -866,7 +889,9 @@ def array_contains_any_queries(db):
866889 cities_ref = db .collection ("cities" )
867890
868891 query = cities_ref .where (
869- "regions" , "array_contains_any" , ["west_coast" , "east_coast" ]
892+ filter = FieldFilter (
893+ "regions" , "array_contains_any" , ["west_coast" , "east_coast" ]
894+ )
870895 )
871896 return query
872897 # [END firestore_query_filter_array_contains_any]
@@ -876,7 +901,7 @@ def in_query_without_array(db):
876901 # [START firestore_query_filter_in]
877902 cities_ref = db .collection ("cities" )
878903
879- query = cities_ref .where ("country" , "in" , ["USA" , "Japan" ])
904+ query = cities_ref .where (filter = FieldFilter ( "country" , "in" , ["USA" , "Japan" ]) )
880905 return query
881906 # [END firestore_query_filter_in]
882907
@@ -885,7 +910,9 @@ def in_query_with_array(db):
885910 # [START firestore_query_filter_in_with_array]
886911 cities_ref = db .collection ("cities" )
887912
888- query = cities_ref .where ("regions" , "in" , [["west_coast" ], ["east_coast" ]])
913+ query = cities_ref .where (
914+ filter = FieldFilter ("regions" , "in" , [["west_coast" ], ["east_coast" ]])
915+ )
889916 return query
890917 # [END firestore_query_filter_in_with_array]
891918
0 commit comments