在不同的字段上使用过滤器对字段进行Firestore查询顺序 [英] Firestore query order on field with filter on a different field

查看:57
本文介绍了在不同的字段上使用过滤器对字段进行Firestore查询顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Cloud Firestore中的查询条件有问题.

I have a problem with query condition in google Cloud Firestore.

任何人都可以帮助我.

下面是我的代码,以获得第一个以HA_开头并按ID DESC排序的文档

Below is my code to get first Document start with HA_ and order by 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为"HA_"或"XE_"开头的文章的最后ID

I want to get last id of article with articleId start with "HA_" or "XE_"

上图的EX:

  • if(articleId以"HA_"开头)=>返回ID为831的对象
  • if(articleId以"XE_"开头)=>返回ID为833的对象

现在我得到一个错误

java.util.concurrent.ExecutionException:com.google.api.gax.rpc.InvalidArgumentException:io.grpc.StatusRuntimeException:INVALID_ARGUMENT:不等式过滤器属性和第一排序顺序必须相同:articleId和id

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

推荐答案

如以下错误所述:

不平等过滤条件属性和第一排序顺序必须相同:articleId和id

inequality filter property and first sort order must be the same: articleId and id

因此,对于articleIdid,您不能使用不同的属性同时过滤元素并对其进行排序.

So you cannot filter your elements and sort them in the same time, using different properties, in your case articleId and id.

  • 范围过滤器和一阶按不同字段

    • Range filter and first orderBy on different fields

    citiesRef.whereGreaterThan("population", 100000).orderBy("country"); //Invalid
    

  • 因此,要解决此问题,您应该对同一文档属性进行过滤和排序.

    So to solve this, you should filter and order on the same document property.

    这篇关于在不同的字段上使用过滤器对字段进行Firestore查询顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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