1 回答

TA贡献1906条经验 获得超10个赞
感谢最新的Firestore补丁(2019年3月13日)
Firestore 的 FieldValue 类现在承载一个增量方法,该方法以原子方式更新 firestore 数据库中的数字文档字段。您可以将此 FieldValue sentinel 与对象的(使用 mergeOptions true)或方法一起使用。setupdateDocumentReference
用法如下(来自官方文档,这就是全部):
DocumentReference washingtonRef = db.collection("cities").document("DC");
// Atomically increment the population of the city by 50.
washingtonRef.update("population", FieldValue.increment(50));
如果您想知道,可以从消防商店的版本获得。为方便起见,Gradle 依赖项配置是18.2.0implementation 'com.google.firebase:firebase-firestore:18.2.0'
注: 递增操作对于实现计数器很有用,但请记住,每秒只能更新一次单个文档。如果您需要更新高于此速率的计数器,请参阅分布式计数器页面。
FieldValue.increment()是纯粹的“服务器”端(发生在 firestore 中),因此您无需向客户端公开当前值。
添加回答
举报