Firestore whereEqualTo、orderBy 和 limit(1) 不起作用 [英] Firestore whereEqualTo, orderBy and limit(1) not working

查看:18
本文介绍了Firestore whereEqualTo、orderBy 和 limit(1) 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的锻炼收藏中查询最新锻炼.意思是我用 whereEqualTo 查询我的routineKey,按 Started TimeStamp 按降序对其进行排序,然后限制为 1,然后获取锻炼的第一个键/Id.

I want to query my Workout Collection for the latest workout from a routine. Meaning I query with whereEqualTo my routineKey, order it by the Started TimeStamp in descending order and then limit to 1 and then take the this 1st Key/Id of the Workout.

然而这行不通.whereEqualToorderBy 单独工作但不合并.我做错了什么?

However this does not work. whereEqualTo and orderBy work separately but not combined. What am I doing wrong?

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
                }
            });

推荐答案

除非您创建一个index .这可以通过在您的 Firebase 控制台 中手动创建来完成,或者如果您使用的是 Android Studio,您会在您的 logcat 中找到一条听起来像这样的消息:

This query will not work unless you create an index for it. This can be done, by creating it manually in your Firebase Console or if you are using Android Studio, you'll find in your logcat a message that sounds like this:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...

您只需点击该链接或将 URL 复制并粘贴到网络浏览器中,您的索引就会自动创建.

You can simply click on that link or copy and paste the URL into a web browser and your index will be created automatically.

这篇关于Firestore whereEqualTo、orderBy 和 limit(1) 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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