我需要过滤我从我的 android app
中的 firestore
获取的文档列表。这是查询。
query = FirebaseFirestore.getInstance()
.collection("students")
.whereLessThan("mAge",25)
.whereGreaterThan("mAge",20)
.whereGreaterThan("mGrades",20);
但是,我在日志中收到一个错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{dsardy.in.acchebacche/dsardy.in.acchebacche.MainActivity}: java.lang.IllegalArgumentException: All where filters other than whereEqualTo() must be on the same field. But you have filters on 'mAge' and 'mGrades'
这能实现吗?具有两个或多个字段大于某些值的过滤器是一个重要且通用的查询,firestore
必须有一些东西来解决这个问题。
最佳答案
Firestore 允许链接多个 where() 方法来创建更具体的查询,但仅限于同一字段。正如您可能在 official documentation 中看到的那样禁止对不同字段进行范围过滤。
为了达到你想要的效果,你需要查询你的数据库两次,一次使用 .whereGreaterThan("mAge",20)
来过滤数据,第二次使用 .whereGreaterThan("mGrades",20)
但您不能在同一个查询中使用它们。
实现它的另一种方法是存储一个可能适合查询的特殊字段,尽管在现实世界的应用程序中几乎不可能存储用户可能查询数据的每一种方式。
关于java - 如何使用 whereGreaterthan() 过滤器为两个不同的字段构建适用于 Android 的 firestore 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47588160/