使用 firestore 进行多查询和分页
multi query and pagination with 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)
没有返回错误。是 firestore 的错误还是我这边的错误。
一个 Firestore 查询只能有一个范围条件。
You can combine where()
filters with orderBy()
and limit()
.
However, if you have a filter with a range comparison (<
, <=
, >
, >=
), your first ordering must be on the same field:
Invalid: Range filter and first orderBy on different fields
citiesRef.where("population", ">", 100000).orderBy("country")
正如 Frank 指出的那样,到目前为止,firestore 不允许组合不同属性的范围。我希望 google 有一天会解决这个问题,拥有多个范围过滤器似乎是任何数据库的一个非常重要的功能。
这也是一个相当丑陋的解决方案,但我想您可以省略 .where('price', '>=', 2)
部分,然后在客户端过滤掉数据。
firebase 在 Pagination & Query and query data 上有文档。我们必须使用 startAt() 或 startAfter() 方法来定义查询的起点。同样,使用 endAt() 或 endBefore() 方法定义查询结果的终点。
示例:
获取所有人口 >= 1,000,000 的城市,按人口排序,
db.collection("cities")
.orderBy("population")
.startAt(1000000);
并获取所有人口 <= 1,000,000 的城市,按人口排序,
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
// ...
}
});
检查 this 使用 FireStore 的示例应用,Angular 8 个应用具有分页
使用查询
限制()
orderBy()
startAt()
endBefore()
startAfter()
我正在尝试使用 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 的错误还是我这边的错误。
一个 Firestore 查询只能有一个范围条件。
You can combine
where()
filters withorderBy()
andlimit()
.However, if you have a filter with a range comparison (
<
,<=
,>
,>=
), your first ordering must be on the same field:Invalid: Range filter and first orderBy on different fields
citiesRef.where("population", ">", 100000).orderBy("country")
正如 Frank 指出的那样,到目前为止,firestore 不允许组合不同属性的范围。我希望 google 有一天会解决这个问题,拥有多个范围过滤器似乎是任何数据库的一个非常重要的功能。
这也是一个相当丑陋的解决方案,但我想您可以省略 .where('price', '>=', 2)
部分,然后在客户端过滤掉数据。
firebase 在 Pagination & Query and query data 上有文档。我们必须使用 startAt() 或 startAfter() 方法来定义查询的起点。同样,使用 endAt() 或 endBefore() 方法定义查询结果的终点。
示例: 获取所有人口 >= 1,000,000 的城市,按人口排序,
db.collection("cities")
.orderBy("population")
.startAt(1000000);
并获取所有人口 <= 1,000,000 的城市,按人口排序,
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
// ...
}
});
检查 this 使用 FireStore 的示例应用,Angular 8 个应用具有分页
使用查询
限制()
orderBy()
startAt()
endBefore()
startAfter()