[英]Cloud Firestore , nodejs (cloud functions), synchronize access
What is the Java equivalent of 'synchronize' in Firestore, nodejs (cloud functions)? Firestore、nodejs(云函数)中“同步”的 Java 等价物是什么?
Suppose I keep a counter in the db and I want to increment it by 1, so I have to read the value, increment it, and store it back.假设我在数据库中保留一个计数器并且我想将它递增 1,所以我必须读取该值,递增它,然后将它存储回去。
How do I make sure the counter value didn't change by other party, between the read and write action?如何确保在读写操作之间计数器值没有被其他方更改?
How do I make sure the counter value didn't change by other party, between the read and write action?
如何确保在读写操作之间计数器值没有被其他方更改?
There are two ways in which you can solve this problem.有两种方法可以解决此问题。
The first one would be to use a transaction , which is an operation where you read a value from a document, you increment it and then write the document back.第一个是使用事务,这是一种操作,您从文档中读取一个值,递增它,然后将文档写回。
The second solution would be to use the FieldValue.increment(50) operator:第二种解决方案是使用FieldValue.increment(50)运算符:
An increment operation increases or decreases the current value of a field by the given amount.
增量操作将字段的当前值增加或减少给定的数量。 If the field does not exist or if the current field value is not a numeric value, the operation sets the field to the given value.
如果字段不存在或当前字段值不是数值,则该操作将字段设置为给定值。
If you need at some point in time to decrement a value, simply pass a negative value to the increment()
function:如果您需要在某个时间点递减一个值,只需将一个负值传递给
increment()
函数:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.