firebase cloud functions how to add data to firestore code example

Example 1: simple firestore cloud function update document

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

admin.initializeApp();

export const resetCounter = functions.https.onRequest((req, res) => {
    const resetRef =  admin.firestore()
    .collection('collectionName')
    .doc('documentName')
    resetRef.get().then((doc) => {
        if(doc.exists){
            resetRef.update({counter: 0})
              .catch(err => {
            console.log("Error",err)
            res.send("500");
          })
       }
    }).catch(err=>{
      	//Internal server error
      	console.log("Error",err)
        res.send("500");
    });
  //Successful operation
  res.send("200");
});

Example 2: document.set() firebasefirestore java

// Create an initial document to update.var frankDocRef = db.collection("users").doc("frank");frankDocRef.set({    name: "Frank",    favorites: { food: "Pizza", color: "Blue", subject: "recess" },    age: 12});// To update age and favorite color:db.collection("users").doc("frank").update({    "age": 13,    "favorites.color": "Red"});