Answers for "firestore update document field"

4

Firestore increment field

const userRef = db.collection('documents').doc('document_id');
const increment = firebase.firestore.FieldValue.increment(1);

userRef.update({ FieldToIncrease: increment });
Posted by: Guest on July-03-2020
5

firestore set a document

let data = {
  name: 'Los Angeles',
  state: 'CA',
  country: 'USA'
};

// Add a new document in collection "cities" with ID 'LA'
let setDoc = db.collection('cities').doc('LA').set(data);
Posted by: Guest on June-05-2020
2

firestore update array

let washingtonRef = db.collection('cities').doc('DC');

// Atomically add a new region to the "regions" array field.
let arrUnion = washingtonRef.update({
  regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
});
// Atomically remove a region from the "regions" array field.
let arrRm = washingtonRef.update({
  regions: admin.firestore.FieldValue.arrayRemove('east_coast')
});
Posted by: Guest on June-05-2020
1

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");
    });
Posted by: Guest on July-21-2020
1

firestore create document with auto id

// Add a new document with a generated id.
db.collection("cities").add({
    name: "Tokyo",
    country: "Japan"
})
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});
---------------------------
//If you want to specify id then use .set() 
// e.x   /
db.collection("cities").doc("TYO").set({
    name: "Tokyo",
    country: "Japan"
})
Posted by: Guest on January-16-2021
0

document.set() firebasefirestore java

var washingtonRef = db.collection("cities").doc("DC");
// Atomically add a new region to the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});
// Atomically remove a region from the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayRemove("east_coast")
});
Posted by: Guest on July-20-2020

Code answers related to "firestore update document field"

Browse Popular Code Answers by Language