@@ -269,6 +269,24 @@ function updateDocumentArray(db) {
269269 } ) ;
270270}
271271
272+ function updateDocumentIncrement ( db ) {
273+ // [START update_document_increment]
274+ var admin = require ( 'firebase-admin' ) ;
275+ // ...
276+ var washingtonRef = db . collection ( 'cities' ) . doc ( 'DC' ) ;
277+
278+ // Atomically incrememnt the population of the city by 50.
279+ // Note that increment() with no arguments increments by 1.
280+ var popIncrement = washingtonRef . update ( {
281+ population : admin . firestore . FieldValue . increment ( 50 )
282+ } ) ;
283+ // [END update_document_increment]
284+
285+ return popIncrement . then ( res => {
286+ console . log ( 'Increment: ' + res ) ;
287+ } ) ;
288+ }
289+
272290function updateDocumentMany ( db ) {
273291 // [START update_document_many]
274292 var cityRef = db . collection ( 'cities' ) . doc ( 'DC' ) ;
@@ -391,7 +409,9 @@ function transaction(db) {
391409 var transaction = db . runTransaction ( t => {
392410 return t . get ( cityRef )
393411 . then ( doc => {
394- // Add one person to the city population
412+ // Add one person to the city population.
413+ // Note: this could be done without a transaction
414+ // by updating the population using FieldValue.increment()
395415 var newPopulation = doc . data ( ) . population + 1 ;
396416 t . update ( cityRef , { population : newPopulation } ) ;
397417 } ) ;
@@ -992,7 +1012,11 @@ describe('Firestore Smoketests', () => {
9921012 return updateDocumentArray ( db ) ;
9931013 } ) ;
9941014
995- it ( 'should update many document' , ( ) => {
1015+ it ( 'should update a document using numeric transforms' , ( ) => {
1016+ return updateDocumentIncrement ( db ) ;
1017+ } ) ;
1018+
1019+ it ( 'should update many documents' , ( ) => {
9961020 return updateDocumentMany ( db ) ;
9971021 } ) ;
9981022
0 commit comments