45

Im working with React native and react-native-firebase

My objective is to add multiple docs(objects) to a collection at once. Currently, I have this:

const array = [
  {
     name: 'a'
  },{
    name: 'b'
  }
]
array.forEach((doc) => {
  firebase.firestore().collection('col').add(doc);
}

This triggers an update on other devices for each update made to the collection. How can I batch these docs together for ONE update?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Kolby Watson
  • 483
  • 1
  • 4
  • 6

5 Answers5

91

You can create batch write like

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

in you array add updates

array.forEach((doc) => {
  var docRef = db.collection("col").doc(); //automatically generate unique id
  batch.set(docRef, doc);
});

finally you have to commit that

batch.commit()
Dominik Šimoník
  • 1,442
  • 10
  • 17
20

You can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.

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

array.forEach((doc) => {

  batch.set(db.collection('col').doc(), doc);
}
// Commit the batch
batch.commit().then(function () {
    // ...
});
Cappittall
  • 3,300
  • 3
  • 15
  • 23
7

Version 9 of Web API is slightly different, the docs include this example:

import { writeBatch, doc } from "firebase/firestore"; 

// Get a new write batch
const batch = writeBatch(db);

// Set the value of 'NYC'
const nycRef = doc(db, "cities", "NYC");
batch.set(nycRef, {name: "New York City"});

// Update the population of 'SF'
const sfRef = doc(db, "cities", "SF");
batch.update(sfRef, {"population": 1000000});

// Delete the city 'LA'
const laRef = doc(db, "cities", "LA");
batch.delete(laRef);

// Commit the batch
await batch.commit();
Gareth
  • 311
  • 3
  • 6
5

The batch from database also has a create function that adds a new document in a collection and throws an error if there is already a document. we just need the reference to the document. Please note that this function exists in admin sdk of firebase.

const batch = db.batch();
await users.map(async (item)=> {
    const collectionRef = await db.collection(COLLECTION_NAME).doc();
    batch.create(collectionRef, item);
  });

const result = await batch.commit();

0

A batched write can contain up to 500 operations. Each operation in the batch counts separately towards your Cloud Firestore usage.

Note: For bulk data entry, use a server client library with parallelized individual writes. Batched writes perform better than serialized writes but not better than parallel writes. You should use a server client library for bulk data operations and not a mobile/web SDK.

Hypermona
  • 31
  • 3
  • Tell us how did you come up with this. Did you read the answer below? – Mises Oct 26 '22 at 04:02
  • @Mises because some people think that writing in batches might save their write quota, but it is not firebase charges you separately for each writes object in the batch. – Hypermona Oct 26 '22 at 05:54
  • @Mises it is not the answer but relevant to the answer, thank you. – Hypermona Oct 26 '22 at 05:55