我想查询我的锻炼 Collection 以获取例程中的最新锻炼。这意味着我用 whereEqualTo
查询我的 routineKey,按开始时间戳降序排列,然后限制为 1,然后获取锻炼的第一个键/ID。
但是这不起作用。 whereEqualTo
和 orderBy
单独工作但不组合。我做错了什么?
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 Console 中手动创建来完成或者,如果您使用的是 Android Studio,您会在您的 logcat 中找到一条听起来像这样的消息:
FAILED_PRECONDITION: The query requires an index. You can create it here: ...
您只需单击该链接或将 URL 复制并粘贴到网络浏览器中,系统就会自动创建您的索引。
关于java - Firestore whereEqualTo、orderBy 和 limit(1) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50305328/