hai*_*aim 4 firebase google-cloud-firestore
我正在尝试使用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)
Run Code Online (Sandbox Code Playgroud)
没有错误返回.是firestore还是我的错误?
在firebase上有关于分页和查询以及查询数据的文档。我们必须使用startAt()或startAfter()方法来定义查询的起点。同样,使用endAt()或endBefore()方法来定义查询结果的终点。
示例:要获得人口大于等于1,000,000的所有城市(按人口排序),
db.collection("cities")
.orderBy("population")
.startAt(1000000);
Run Code Online (Sandbox Code Playgroud)
并获得所有人口<= 1,000,000的城市(按人口排序),
db.collection("cities")
.orderBy("population")
.endAt(1000000);
Run Code Online (Sandbox Code Playgroud)
所以分页应该使用这种方法
// 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
// ...
}
});
Run Code Online (Sandbox Code Playgroud)
Firestore查询只能具有单个范围条件.
从查询文档:
您可以将
where()
过滤器与orderBy()
和组合使用limit()
.但是,如果你有一个范围比较器(过滤芯
<
,<=
,>
,>=
),你的第一顺序必须是在同一个领域:无效:范围过滤器和第一个订单在不同的字段上
Run Code Online (Sandbox Code Playgroud)citiesRef.where("population", ">", 100000).orderBy("country")
归档时间: |
|
查看次数: |
11377 次 |
最近记录: |