为了账号安全,请及时绑定邮箱和手机立即绑定

增加火库火库中的现有值

增加火库火库中的现有值

婷婷同学_ 2022-08-17 10:40:52
有没有办法增加消防店的现有价值?我正在尝试什么。 数据库结构    FirebaseFirestore.getInstance()            .collection("notifications")            .document(response.getString("multicast_id"))            .set(notificationData);    FirebaseFirestore.getInstance()            .collection("users")            .document(post.userId)            .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {        @Override        public void onComplete(@NonNull Task<DocumentSnapshot> task) {            if (task.isSuccessful()) {                int followers = Integer.valueOf(task.getResult().getData().get("followers").toString());                FirebaseFirestore.getInstance()                        .collection("users")                        .document(post.userId).update("followers", followers++).addOnCompleteListener(new OnCompleteListener<Void>() {                    @Override                    public void onComplete(@NonNull Task<Void> task) {                    }                });            }        }    });users  - data....  - followers(number)根据此答案函数可用于递增或递减数值字段值版本 5.9.0 - 2019年3月14日添加了 FieldValue.increment(),可以在 update() 和 set(..., {merge:true}) 中使用,以安全地递增或递减数值字段值,而无需事务。https://firebase.google.com/support/release-notes/js是否有任何功能可用于增加消防商店Android中的现有价值?
查看完整描述

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 中),因此您无需向客户端公开当前值。


查看完整回答
反对 回复 2022-08-17
  • 1 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号