我在 google Cloud Firestore 中的查询条件有问题。
任何人都可以帮助我。
以下是我获取第一个以 HA_ 开头的文档并按 ID DESC 排序的代码
public Article getLastArticleProvider() {
ApiFuture<QuerySnapshot> query = firebaseDB.getTblArticles()
.whereGreaterThanOrEqualTo("articleId", "HA_")
.orderBy("id", Query.Direction.DESCENDING)
.limit(1)
.get();
QuerySnapshot snapshotApiFuture;
Article article = null;
try {
snapshotApiFuture = query.get();
List<QueryDocumentSnapshot> documents = snapshotApiFuture.getDocuments();
for (QueryDocumentSnapshot document : documents) {
article = document.toObject(Article.class);
}
} catch (InterruptedException | ExecutionException e) {
return null;
}
return article;
}
我想获取文章的最后一个 ID,文章 ID 以“HA_”或“XE_”开头
例如上图:
现在我收到一个错误
java.util.concurrent.ExecutionException: com.google.api.gax.rpc.InvalidArgumentException: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: inequality filter property and first sort order must be the same: articleId and id
最佳答案
正如以下错误所说:
inequality filter property and first sort order must be the same: articleId and id
因此,您不能使用不同的属性过滤元素并同时对它们进行排序,在您的情况下是
articleId
和 id
。还有一个例子说明 不是 如何在 official documentation 中做到这一点:
citiesRef.whereGreaterThan("population", 100000).orderBy("country"); //Invalid
因此,要解决此问题,您应该对同一文档属性进行过滤和排序。
关于java - 字段上的 Firestore 查询顺序与不同字段上的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850287/