我有一个字段表示交易数量。我想增加 1
一旦发生交易。我找不到任何直接执行此操作的方法。
一种方法是先获取用户,然后使用接收到的值 + 1
更新字段。 ,但这太长太慢了。
谁能告诉我任何其他可行的方法?
最佳答案
Firestore 现在有一个名为 FieldValue.increment()
的特定操作符。 .通过将此运算符应用于字段,该字段的值可以作为服务器上的单个操作递增(或递减)。
来自 incrementing a numeric value 上的 Firebase 文档:
var washingtonRef = db.collection('cities').doc('DC'); // Atomically increment the population of the city by 50. washingtonRef.update({ population: firebase.firestore.FieldValue.increment(50) });
所以这个例子将人口增加 50,无论当前值是多少。如果该字段尚不存在(或不是数字),则将其设置为
50
在这种情况下。
关于firebase - 如何增加 Cloud Firestore 中的现有数字字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762923/