1515from time import sleep
1616
1717from google .cloud import firestore
18+ from google .cloud .firestore_v1beta1 import ArrayRemove , ArrayUnion
1819import google .cloud .exceptions
1920
2021
@@ -101,12 +102,14 @@ def add_data_types():
101102
102103# [START custom_class_def]
103104class City (object ):
104- def __init__ (self , name , state , country , capital = False , population = 0 ):
105+ def __init__ (self , name , state , country , capital = False , population = 0 ,
106+ regions = []):
105107 self .name = name
106108 self .state = state
107109 self .country = country
108110 self .capital = capital
109111 self .population = population
112+ self .regions = regions
110113
111114 @staticmethod
112115 def from_dict (source ):
@@ -119,6 +122,9 @@ def from_dict(source):
119122 if u'population' in source :
120123 city .population = source [u'population' ]
121124
125+ if u'regions' in source :
126+ city .regions = source [u'regions' ]
127+
122128 return city
123129 # [END_EXCLUDE]
124130
@@ -136,12 +142,17 @@ def to_dict(self):
136142 if self .population :
137143 dest [u'population' ] = self .population
138144
145+ if self .regions :
146+ dest [u'regions' ] = self .regions
147+
139148 return dest
140149 # [END_EXCLUDE]
141150
142151 def __repr__ (self ):
143- return u'City(name={}, country={}, population={}, capital={})' .format (
144- self .name , self .country , self .population , self .capital )
152+ return (
153+ u'City(name={}, country={}, population={}, capital={}, regions={})'
154+ .format (self .name , self .country , self .population , self .capital ,
155+ self .regions ))
145156# [END custom_class_def]
146157
147158
@@ -150,15 +161,19 @@ def add_example_data():
150161 # [START add_example_data]
151162 cities_ref = db .collection (u'cities' )
152163 cities_ref .document (u'SF' ).set (
153- City (u'San Francisco' , u'CA' , u'USA' , False , 860000 ).to_dict ())
164+ City (u'San Francisco' , u'CA' , u'USA' , False , 860000 ,
165+ [u'west_coast' , u'norcal' ]).to_dict ())
154166 cities_ref .document (u'LA' ).set (
155- City (u'Los Angeles' , u'CA' , u'USA' , False , 3900000 ).to_dict ())
167+ City (u'Los Angeles' , u'CA' , u'USA' , False , 3900000 ,
168+ [u'west_coast' , u'socal' ]).to_dict ())
156169 cities_ref .document (u'DC' ).set (
157- City (u'Washington D.C.' , None , u'USA' , True , 680000 ).to_dict ())
170+ City (u'Washington D.C.' , None , u'USA' , True , 680000 ,
171+ [u'east_coast' ]).to_dict ())
158172 cities_ref .document (u'TOK' ).set (
159- City (u'Tokyo' , None , u'Japan' , True , 9000000 ).to_dict ())
173+ City (u'Tokyo' , None , u'Japan' , True , 9000000 ,
174+ [u'kanto' , u'honshu' ]).to_dict ())
160175 cities_ref .document (u'BJ' ).set (
161- City (u'Beijing' , None , u'China' , True , 21500000 ).to_dict ())
176+ City (u'Beijing' , None , u'China' , True , 21500000 , [ u'hebei' ] ).to_dict ())
162177 # [END add_example_data]
163178
164179
@@ -232,6 +247,18 @@ def get_simple_query():
232247 # [END get_simple_query]
233248
234249
250+ def array_contains_filter ():
251+ db = firestore .Client ()
252+ # [START fs_array_contains_filter]
253+ cities_ref = db .collection (u'cities' )
254+
255+ query = cities_ref .where (u'regions' , u'array_contains' , u'west_coast' )
256+ # [END fs_array_contains_filter]
257+ docs = query .get ()
258+ for doc in docs :
259+ print (u'{} => {}' .format (doc .id , doc .to_dict ()))
260+
261+
235262def get_full_collection ():
236263 db = firestore .Client ()
237264 # [START get_full_collection]
@@ -286,6 +313,21 @@ def update_doc():
286313 # [END update_doc]
287314
288315
316+ def update_doc_array ():
317+ db = firestore .Client ()
318+ # [START fs_update_doc_array]
319+ city_ref = db .collection (u'cities' ).document (u'DC' )
320+
321+ # Atomically add a new region to the 'regions' array field.
322+ city_ref .update ({u'regions' : ArrayUnion ([u'greater_virginia' ])})
323+
324+ # // Atomically remove a region from the 'regions' array field.
325+ city_ref .update ({u'regions' : ArrayRemove ([u'east_coast' ])})
326+ # [END fs_update_doc_array]
327+ city = city_ref .get ()
328+ print (u'Updated the regions field of the DC. {}' .format (city .to_dict ()))
329+
330+
289331def update_multiple ():
290332 db = firestore .Client ()
291333 # [START update_multiple]
@@ -569,7 +611,7 @@ def snapshot_cursors():
569611 # [END fs_start_at_snapshot_query_cursor]
570612 results = start_at_snapshot .limit (10 ).get ()
571613 for doc in results :
572- print ('{}' .format (doc .id ))
614+ print (u '{}' .format (doc .id ))
573615
574616 return results
575617
@@ -674,11 +716,11 @@ def on_snapshot(col_snapshot, changes, read_time):
674716 print (u'Callback received query snapshot.' )
675717 print (u'Current cities in California: ' )
676718 for change in changes :
677- if change .type .name == " ADDED" :
719+ if change .type .name == ' ADDED' :
678720 print (u'New city: {}' .format (change .document .id ))
679- elif change .type .name == " MODIFIED" :
721+ elif change .type .name == ' MODIFIED' :
680722 print (u'Modified city: {}' .format (change .document .id ))
681- elif change .type .name == " REMOVED" :
723+ elif change .type .name == ' REMOVED' :
682724 print (u'Removed city: {}' .format (change .document .id ))
683725
684726 col_query = db .collection (u'cities' ).where (u'state' , u'==' , u'CA' )
0 commit comments