Firestore whereEqualTo,orderBy和limit(1)不起作用 [英] Firestore whereEqualTo, orderBy and limit(1) not working
问题描述
我想从常规查询我的锻炼集合中的最新锻炼。意思是我用 whereEqualTo
我的routineKey查询,按开始时间顺序按降序排序,然后限制为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.
然而这不起作用。 whereEqualTo
和 orderBy
单独工作但未合并。我做错了什么?
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
}
});
推荐答案
除非您创建索引。这可以通过在 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进入web broswer,你的索引将自动创建。
You can simply click on that link or copy and paste the url into a web broswer and you index will be created automatically.
这篇关于Firestore whereEqualTo,orderBy和limit(1)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!