使用Firestore进行多查询和分页 [英] multi query and pagination with firestore

查看:148
本文介绍了使用Firestore进行多查询和分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用firestore实现多查询和分页,但是一旦添加<或>查询光标不起作用.

I am trying to implement multi query and pagination with firestore, but once I add < or > to the query the cursor not working.

//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中出错还是在我身上?

no error returned. is it bug with firestore or on my end.

推荐答案

firebase上的文档位于分页&查询查询数据.我们必须使用 startAt() startAfter()方法来定义查询的起点.同样,使用 endAt() endBefore()方法来定义查询结果的终点.

There has documentation at firebase on Pagination & Query and query data. We have to use the startAt() or startAfter() methods to define the start point for a query. Similarly, use the endAt() or endBefore() methods to define an end point for your query results.

示例: 要获得所有人口大于等于1,000,000的城市(按人口排序),

Example: To get all cities with a population >= 1,000,000, ordered by population,

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

并获得所有人口<= 1,000,000,按人口排序的城市,

and to get all cities with a population <= 1,000,000, ordered by population,

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
            // ...
        }
    });

这篇关于使用Firestore进行多查询和分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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