【问题标题】:multi query and pagination with firestore使用 Firestore 进行多查询和分页
【发布时间】:2017-10-17 05:01:20
【问题描述】:

我正在尝试使用 firestore 实现多查询和分页,但是一旦我将 添加到查询中,光标就不起作用了。

//working example:
the doc id i save as propery on the doc
ref.where('category','==' 'cats').where('status', '==', 1).orderBy('id').cursor('last doc id from the returned list').limit(3)

//not working exmple:

ref.where('category','==' 'cats').where('status', '==', 1).orderBy('price').where('price', '>=', 2).where('price', '<=', 55).orderBy('id').cursor('last doc id from the returned list').limit(3)

没有返回错误。是firestore的错误还是我的问题。

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    Pagination & Queryquery data 上有firebase 的文档。我们必须使用 startAt()startAfter() 方法来定义查询的起点。同样,使用 endAt()endBefore() 方法来定义查询结果的终点。

    示例: 要获取所有人口 >= 1,000,000 的城市,按人口排序,

    db.collection("cities")
            .orderBy("population")
            .startAt(1000000);
    

    并得到所有人口

    db.collection("cities")
            .orderBy("population")
            .endAt(1000000);
    

    所以分页应该使用这种方法来完成,例如,

    // Construct query for first 25 cities, ordered by population
    Query first = db.collection("cities")
            .orderBy("population")
            .limit(25);
    
    first.get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {
                // ...
    
                // Get the last visible document
                DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
                        .get(documentSnapshots.size() -1);
    
                // Construct a new query starting at this document,
                // get the next 25 cities.
                Query next = db.collection("cities")
                        .orderBy("population")
                        .startAfter(lastVisible)
                        .limit(25);
    
                // Use the query for pagination
                // ...
            }
        });
    

    【讨论】:

      【解决方案2】:

      Firestore 查询只能有一个范围条件。

      来自documentation on queries

      您可以将where() 过滤器与orderBy()limit() 结合使用。

      但是,如果您有一个带有范围比较的过滤器(&lt;&lt;=&gt;&gt;=),那么您的第一个排序必须在同一字段中:

      无效:范围过滤器和first orderBy在不同字段上

      citiesRef.where("population", ">", 100000).orderBy("country")
      

      【讨论】:

      • 所以我不能在不同的属性上将范围比较(、>=)与 startAfter 结合起来?有什么解决办法吗?
      【解决方案3】:

      检查 this 使用 FireStore 的示例应用和具有分页功能的 Angular 8 应用

      使用查询

      limit()

      orderBy()

      startAt()

      endBefore()

      startAfter()

      【讨论】:

        【解决方案4】:

        正如弗兰克指出的那样,到目前为止,firestore 不允许组合不同属性的范围。我希望谷歌有一天能解决这个问题,拥有多个范围过滤器对于任何数据库来说似乎都是一个非常重要的功能。

        这也是一个相当丑陋的解决方案,但我想你可以省略 .where('price', '&gt;=', 2) 部分,然后在客户端过滤掉数据。

        【讨论】:

        • 谢谢你给我的想法。我会将其从范围更改为大于。我想它总比没有好。我真的不明白为什么他们不实现像mongo这样的所有功能。真的很难用它。
        • @shimonhatoka 他们将来会正确地做到这一点......自从我去年开始使用它以来,他们添加了很多新功能:)
        猜你喜欢
        • 2020-08-25
        • 2019-11-14
        • 2021-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多