firestore add array code example

Example 1: firestore batch add array

var db = firebase.firestore();
var batch = db.batch()

save(docs: any[]) {
  docs.forEach((doc) => {
    var docRef = db.collection("col").doc(); //automatically generate unique id
    batch.set(docRef, doc);
  });
  return batch.commit();
}

Example 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')
});

Example 3: 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")});

Tags:

Misc Example