有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 All-where过滤器带有一个不等式(Firestore Java)

长话短说,我正在为火车时间戳制作一个应用程序,所以我有一个云firestore数据库作为我的数据库。问题的关键是,当用户找到具体的出发时间时,我使用的搜索时间是大于或等于特定的分钟和小时。如果用户试图找到8:30的路线,应用程序应返回到该时间附近的下一条路线。但当用户单击查找路线时,应用程序崩溃。。。以下是错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projekatidemovozom/com.example.projekatidemovozom.SecondActivity}: java.lang.IllegalArgumentException: All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on 'departure.hours' and 'departure.minutes'

这是我从数据库获取数据的代码:

public void initDatabaseConnection() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        CollectionReference timestampsRef = db.collection("timestamps");
        timestampsRef.whereEqualTo("route.cityFrom", cityOd)
                .whereEqualTo("route.cityTo", cityDo)
                .whereGreaterThanOrEqualTo("departure.hours", hours) //ISSUE LINE
                .whereGreaterThanOrEqualTo("departure.minutes", minutes) //ISSUE LINE
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            TimestampModel tm = document.toObject(TimestampModel.class);
                            timestampModelList.add(tm);
                            Log.d(TAG, tm.toString());
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                });
    }

这是我的数据库结构:

data

你知道问题出在哪里吗


共 (1) 个答案

  1. # 1 楼答案

    例外情况说明:

    All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on 'departure.hours' and 'departure.minutes'

    因此,在不同的字段(小时和分钟字段)上不能有两个whereGreaterThanOrEqualTo语句

    事实上,将时间表示为两个字段意味着您的查询不能像您预期的那样工作,因为如果您查询8:30,那么9:00的时间将不匹配,因为0小于30

    取而代之的是,考虑使用单个字段来表示时间(例如,从午夜开始的分钟),这意味着您只能使用单个{{CD1>}。