@@ -248,6 +248,27 @@ function updateDocument(db) {
248
248
} ) ;
249
249
}
250
250
251
+ function updateDocumentArray ( db ) {
252
+ // [START update_document_array]
253
+ var admin = require ( 'firebase-admin' ) ;
254
+ // ...
255
+ var washingtonRef = db . collection ( 'cities' ) . doc ( 'DC' ) ;
256
+
257
+ // Atomically add a new region to the "regions" array field.
258
+ var arrUnion = washingtonRef . update ( {
259
+ regions : admin . firestore . FieldValue . arrayUnion ( 'greater_virginia' )
260
+ } ) ;
261
+ // Atomically remove a region from the "regions" array field.
262
+ var arrRm = washingtonRef . update ( {
263
+ regions : admin . firestore . FieldValue . arrayRemove ( 'east_coast' )
264
+ } ) ;
265
+ // [END update_document_array]
266
+
267
+ return Promise . all ( [ arrUnion , arrRm ] ) . then ( res => {
268
+ console . log ( 'Update array: ' , res ) ;
269
+ } ) ;
270
+ }
271
+
251
272
function updateDocumentMany ( db ) {
252
273
// [START update_document_many]
253
274
var cityRef = db . collection ( 'cities' ) . doc ( 'DC' ) ;
@@ -444,19 +465,24 @@ function exampleData(db) {
444
465
445
466
var setSf = citiesRef . doc ( 'SF' ) . set ( {
446
467
name : 'San Francisco' , state : 'CA' , country : 'USA' ,
447
- capital : false , population : 860000 } ) ;
468
+ capital : false , population : 860000 ,
469
+ regions : [ 'west_coast' , 'norcal' ] } ) ;
448
470
var setLa = citiesRef . doc ( 'LA' ) . set ( {
449
471
name : 'Los Angeles' , state : 'CA' , country : 'USA' ,
450
- capital : false , population : 3900000 } ) ;
472
+ capital : false , population : 3900000 ,
473
+ regions : [ 'west_coast' , 'socal' ] } ) ;
451
474
var setDc = citiesRef . doc ( 'DC' ) . set ( {
452
475
name : 'Washington, D.C.' , state : null , country : 'USA' ,
453
- capital : true , population : 680000 } ) ;
476
+ capital : true , population : 680000 ,
477
+ regions : [ 'east_coast' ] } ) ;
454
478
var setTok = citiesRef . doc ( 'TOK' ) . set ( {
455
479
name : 'Tokyo' , state : null , country : 'Japan' ,
456
- capital : true , population : 9000000 } ) ;
480
+ capital : true , population : 9000000 ,
481
+ regions : [ 'kanto' , 'honshu' ] } ) ;
457
482
var setBj = citiesRef . doc ( 'BJ' ) . set ( {
458
483
name : 'Beijing' , state : null , country : 'China' ,
459
- capital : true , population : 21500000 } ) ;
484
+ capital : true , population : 21500000 ,
485
+ regions : [ 'jingjinji' , 'hebei' ] } ) ;
460
486
// [END example_data]
461
487
462
488
return Promise . all ( [ setSf , setLa , setDc , setTok , setBj ] ) ;
@@ -605,6 +631,18 @@ function queryAndFilter(db) {
605
631
} ) ;
606
632
}
607
633
634
+ function arrayFilter ( db ) {
635
+ var citiesRef = db . collection ( 'cities' ) ;
636
+ // [START array_contains_filter]
637
+ var westCoastCities = citiesRef . where ( 'regions' , 'array-contains' , 'west_coast' ) ;
638
+ // [END array_contains_filter]
639
+
640
+ return westCoastCities . get ( )
641
+ . then ( res => {
642
+ console . log ( 'West Coast get: ' , res ) ;
643
+ } ) ;
644
+ }
645
+
608
646
function orderAndLimit ( db ) {
609
647
var citiesRef = db . collection ( 'cities' ) ;
610
648
// [START order_limit]
@@ -916,6 +954,10 @@ describe('Firestore Smoketests', () => {
916
954
return updateDocument ( db ) ;
917
955
} ) ;
918
956
957
+ it ( 'should update array fields in a document' , ( ) => {
958
+ return updateDocumentArray ( db ) ;
959
+ } ) ;
960
+
919
961
it ( 'should update many document' , ( ) => {
920
962
return updateDocumentMany ( db ) ;
921
963
} ) ;
@@ -962,6 +1004,10 @@ describe('Firestore Smoketests', () => {
962
1004
it ( 'should query and filter' , ( ) => {
963
1005
return queryAndFilter ( db ) ;
964
1006
} ) ;
1007
+
1008
+ it ( 'should query and filter an array' , ( ) => {
1009
+ return arrayFilter ( db ) ;
1010
+ } ) ;
965
1011
966
1012
it ( 'should order and limit' , ( ) => {
967
1013
return orderAndLimit ( db ) ;
0 commit comments