Firestore's Update
and Delete
document methods accept an optional Precondition
argument, to check whether the doc exists or its last update time. Does that check count as an extra document read?
I'd like to add precondition functionality for the Set
method (using a read inside a Transaction
), but that'll involve an extra read, so I wanted to know if there'd be a difference...
I looked up the Firestore documentation, but couldn't find an answer.
Firestore's Update
and Delete
document methods accept an optional Precondition
argument, to check whether the doc exists or its last update time. Does that check count as an extra document read?
I'd like to add precondition functionality for the Set
method (using a read inside a Transaction
), but that'll involve an extra read, so I wanted to know if there'd be a difference...
I looked up the Firestore documentation, but couldn't find an answer.
Share Improve this question edited Feb 17 at 6:34 Alex Mamo 139k18 gold badges168 silver badges201 bronze badges Recognized by Google Cloud Collective asked Feb 17 at 5:00 BoBch27BoBch27 261 silver badge3 bronze badges1 Answer
Reset to default 1Regarding the optional Preconditions
argument, please note it doesn't really matter how you read a document, as long as you read it, you have to pay a read operation in Firestore. That being said, if you need to know if a document exists in Firestore before updating it, you should read it first and check if it exists. Why such an extra step? It's because if you want to update a non-existing document, the update operation will fail.
So in my opinion the best option that you have is to perform such an operation in a Firestore transaction, in which case the update will not fail due to the missing document. So please use the transaction
object in order to achieve that.
For more information, I recommend you check the official documentation:
- https://firebase.google/docs/firestore/manage-data/transactions#transactions