0

Coming from Firebase realtime database I wonder if Cloud Firestore has something equivalent to the updateChildren() function. It can batch updates many nodes at the same time or if it fails none are updated.

Sam Storie
  • 4,444
  • 4
  • 48
  • 74
Erik
  • 5,039
  • 10
  • 63
  • 119

1 Answers1

1

Firestore supports transactions that can update several documents as a unit, or none if any update fails:

https://cloud.google.com/firestore/docs/manage-data/transactions

Here's a snippet from the page referenced:

// Get a new write batch
var batch = db.batch();

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

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

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

// Commit the batch
batch.commit().then(function () {
    // ...
});
Sam Storie
  • 4,444
  • 4
  • 48
  • 74
  • Thanks that´s what i need but one thing the "You can perform batched writes even when the user's device is offline" , is there a way to disable this so the Batched writes fail if device is offline? I see the "Transactions will fail when the client is offline." I have like 10 different writes(no reads) that must be done as a batch or not . The Firebase realtime databas have the `keepSynced(true)` maybe Cloud Firestore have something like that – Erik Nov 01 '17 at 15:23