Use a field value to update another field in same document

Viewed 269

I am using Firebase Firestore and have documents with two fields:

field X
field Y

They are both numbers. Now I want to copy the value from X to Y. Of course I can just read the document to have the value X and then do a write operation on the same document and update field Y.

My question is, if there is a possibility to do this with just an update-operation as I couldn't find anything related but it seems to me like this might be something that's implemented by Firebase.

Thank you

2 Answers

Unfortunately, something like you described is not possible. One would simply read the document and update it with the read value.

Depending on your needs, you can use transactions. For instance, I used it for the rating functionality, and actually, you might find this thread helpful.

Related