是否可以在Firestore中为数字字段设置最大值? [英] Is it possible to set a maximum value for a number field in Firestore?

查看:47
本文介绍了是否可以在Firestore中为数字字段设置最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想增加保存在Firestore中的字段,但该字段不得超过某个值.可以为一个字段设置最大值吗?

I want to increment a field saved in Firestore, but it should not exceed a certain value. Is it possible to set a maximum value for a field?

如果没有,那么在Kotlin中实现这种限制的首选方式是什么?

If not, what's the preferred way to implement such a limit in Kotlin?

推荐答案

如果您要询问 FieldValue.increment(),则不可能.您应该改用交易来阅读文档,计算新的值限制在适当范围内,然后将其写回到文档中.

If you're asking about FieldValue.increment(), it's not possible. You should instead use a transaction to read the document, compute a new value within appropriate limits, then write it back to the document.

如果您要问是否可以完全拒绝不良值,无论它们是如何编写的,都可以使用安全规则.后端代码会绕过安全规则.

If you're asking if it's possible to reject bad values altogether, no matter how they got written, you can do that with security rules, assuming that the write is coming from a web or mobile client. Backend code bypasses security rules.

这篇关于是否可以在Firestore中为数字字段设置最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆