update all documents in collection firestore when user changes name code example

Example: firestore cloud function update documents

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp();

export const setProductsToExpired = functions.https.onRequest(async(request, response) => {
    const expiredProducts = await admin.firestore()
      .collection('products')
      .where('timestamp','<=', admin.firestore.Timestamp.now())
      .get();
    
    const batch = admin.firestore().batch();
 
    expiredProducts.forEach(doc => {
      batch.update(doc.ref,'expired',true);
    });
    
    await batch.commit();
    //Successful operation
    response.send("200");
    });

Tags:

Misc Example