awariat
awariat

Reputation: 382

Firestore "set" and maximum write rate to a document

According docs there is 1 write per second per document. Official example:

db.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})

Is there 3 writes? or one? Shouldn't they use batch writing? If I want update this document (all fields) it looks like I should use batch or transaction. Do I need use batch if I set the same fields in above documents instead update.

Upvotes: 0

Views: 139

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138814

Is there 3 writes? or one?

The code that you have shared is considered a single write operation since all properties are changed within the same set() function call.

Shouldn't they use batch writing?

In this case, no. Batch writes are useful when setting/updating/deleting multiple documents that exist within different references.

If I want update this document (all fields) it looks like I should use batch or transaction.

Not at all. You should use a bacth as explained above and a transaction when you expected that your app will be used in a in a multi user environment and a document can be changed by multiple users simultaneously. Transaction are used to always have consistent data.

Do i need use batch if I set the same fields in above documents instead update.

No, the way you are doing is the way the docs states.

Upvotes: 1

Related Questions