Multi-path update with Firestore

Viewed 2980

Everywhere I read about Firestore it is stated that it requires less denormalization than Realtime Firebase. I guess this is because it is a document database where you can point at specific documents and only retrieve that amount of data (?).

However, I wonder how to manage a situation where denormalization would still be useful (e.g., we can save a query to the document containing the full information data by storing that same value on other documents as well). If an update of that value is then required, is there something like the Realtime Firebase multi-path update (to update the value at every document) to solve this issue?

1 Answers

I think what you wanted to say is that 'firestore requires less denormalisation than real time database' (both are Firebase products responsible for storing data)'. I don't think that this claim is necessarily true because it all comes down to the architecture of your data. Firestore enforces you to obey some good practises but that does not mean you can get similar architecture in the real time database.

Updating Denormalised Data

You can use batch writes to update denormalised data located at the different paths. Note however, that you can only update up to 500 entities in a single batch.

If you do not need to read any documents in your operation set, 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.

Example from the Firebase Firestore Documentation

// 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 () {
    // ...
});

Note: It might not be clear form the code but none of the writes will be performed on Firestore until commit method is invoked.

Related