Firestore whereEqualTo,orderBy和limit(1)不起作用

Jon*_*nas 6 java android firebase google-cloud-firestore

我想从常规查询我的锻炼集合中的最新锻炼.这意味着我使用whereEqualTo我的routineKey进行查询,按照开始时间顺序按降序排序,然后限制为1,然后取出锻炼的第一个键/ Id.

但这不起作用.whereEqualToorderBy单独工作但不合并.我究竟做错了什么?

fm.getColRefWorkout().whereEqualTo("routineKey", routineKey).orderBy("startTimeStamp", Query.Direction.DESCENDING).limit(1).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { 
                @Override
                public void onSuccess(QuerySnapshot documentSnapshots) {
                    workoutKey = documentSnapshots.getDocuments().get(0).getId();
                    //To stuff with this workoutKey
                }
            });
Run Code Online (Sandbox Code Playgroud)

Ale*_*amo 11

除非为其创建索引,否则此查询将不起作用.这可以通过在Firebase控制台中手动创建,或者如果您使用的是Android Studio,可以在logcat中找到如下所示的消息:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...
Run Code Online (Sandbox Code Playgroud)

您只需单击该链接或将该URL复制并粘贴到Web浏览器中,即可自动创建索引.