15
15
from time import sleep
16
16
17
17
from google .cloud import firestore
18
+ from google .cloud .firestore_v1beta1 import ArrayRemove , ArrayUnion
18
19
import google .cloud .exceptions
19
20
20
21
@@ -101,12 +102,14 @@ def add_data_types():
101
102
102
103
# [START custom_class_def]
103
104
class 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 = []):
105
107
self .name = name
106
108
self .state = state
107
109
self .country = country
108
110
self .capital = capital
109
111
self .population = population
112
+ self .regions = regions
110
113
111
114
@staticmethod
112
115
def from_dict (source ):
@@ -119,6 +122,9 @@ def from_dict(source):
119
122
if u'population' in source :
120
123
city .population = source [u'population' ]
121
124
125
+ if u'regions' in source :
126
+ city .regions = source [u'regions' ]
127
+
122
128
return city
123
129
# [END_EXCLUDE]
124
130
@@ -136,12 +142,17 @@ def to_dict(self):
136
142
if self .population :
137
143
dest [u'population' ] = self .population
138
144
145
+ if self .regions :
146
+ dest [u'regions' ] = self .regions
147
+
139
148
return dest
140
149
# [END_EXCLUDE]
141
150
142
151
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 ))
145
156
# [END custom_class_def]
146
157
147
158
@@ -150,15 +161,19 @@ def add_example_data():
150
161
# [START add_example_data]
151
162
cities_ref = db .collection (u'cities' )
152
163
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 ())
154
166
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 ())
156
169
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 ())
158
172
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 ())
160
175
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 ())
162
177
# [END add_example_data]
163
178
164
179
@@ -232,6 +247,18 @@ def get_simple_query():
232
247
# [END get_simple_query]
233
248
234
249
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
+
235
262
def get_full_collection ():
236
263
db = firestore .Client ()
237
264
# [START get_full_collection]
@@ -286,6 +313,21 @@ def update_doc():
286
313
# [END update_doc]
287
314
288
315
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
+
289
331
def update_multiple ():
290
332
db = firestore .Client ()
291
333
# [START update_multiple]
@@ -569,7 +611,7 @@ def snapshot_cursors():
569
611
# [END fs_start_at_snapshot_query_cursor]
570
612
results = start_at_snapshot .limit (10 ).get ()
571
613
for doc in results :
572
- print ('{}' .format (doc .id ))
614
+ print (u '{}' .format (doc .id ))
573
615
574
616
return results
575
617
@@ -674,11 +716,11 @@ def on_snapshot(col_snapshot, changes, read_time):
674
716
print (u'Callback received query snapshot.' )
675
717
print (u'Current cities in California: ' )
676
718
for change in changes :
677
- if change .type .name == " ADDED" :
719
+ if change .type .name == ' ADDED' :
678
720
print (u'New city: {}' .format (change .document .id ))
679
- elif change .type .name == " MODIFIED" :
721
+ elif change .type .name == ' MODIFIED' :
680
722
print (u'Modified city: {}' .format (change .document .id ))
681
- elif change .type .name == " REMOVED" :
723
+ elif change .type .name == ' REMOVED' :
682
724
print (u'Removed city: {}' .format (change .document .id ))
683
725
684
726
col_query = db .collection (u'cities' ).where (u'state' , u'==' , u'CA' )
0 commit comments