简体   繁体   English

Cloud Firestore、nodejs(云函数)、同步访问

[英]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.

相关问题 如何通过Cloud Functions上传文件到Cloud Storage,并使用Firestore控制对Cloud Storage的访问? - How can I upload files to Cloud Storage through Cloud Functions and use Firestore to control access to Cloud Storage? Firestore 云功能在没有部署功能的情况下不会触发 - Firestore cloud functions not triggering without deploying functions 如何在云 function 或 nodejs 中转换 Firestore 时间戳 - How to convert firestore TimeStamp in cloud function or nodejs 与 firebase 云函数中的 firebase firestore 交互 - Interacting with firebase firestore from firebase cloud functions 使用 Cloud Functions 执行 Firestore 操作是否更安全? - Is it safer to do Firestore operations using Cloud Functions? 如何将 Firestore 侦听器功能部署到 GCP Cloud? - How to deploy Firestore listener functions to GCP Cloud? 在本地测试 Cloud Functions 时 Cloud Firestore 模拟器未运行 - Cloud Firestore emulator not running when testing Cloud Functions locally 部署 Cloud FireStore 云功能时出现身份验证错误 - Auth error when deploying Cloud FireStore cloud functions Firestore中的Cloud Functions FieldValue增量TypeError - Cloud Functions FieldValue increment TypeError in Firestore Cloud Functions 从 firestore 路径获取列表 - Cloud Functions get list from firestore path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM