有没有办法在查询firestore数据时设置范围?

nil*_*ils 2 python firebase-admin google-cloud-firestore

我有一个文档集合,所有文档都有随机 id 和一个名为 的字段date

docs = collection_ref.order_by(u'date', direction=firestore.Query.ASCENDING).get()
Run Code Online (Sandbox Code Playgroud)

想象一下我将搜索限制为前十个

docs = collection_ref.order_by(date', direction=firestore.Query.ASCENDING).limit(10).get()
Run Code Online (Sandbox Code Playgroud)

当我想要获取 11 到 20 之间的项目时,如何继续下一个查询?

shm*_*les 9

您可以使用offset(),但每个跳过的文档都算作一次阅读。例如,如果您这样做query.offset(10).limit(5),您将需要支付 15 次读取的费用:10 次偏移 + 您实际获得的 5 次。

如果您想避免不必要的读取,请使用startAt()startAfter()

示例(Java,抱歉,不要说 python。这里有文档链接):

QuerySnapshot querySnapshot = // your query snapshot

List<DocumentSnapshot> docs = querySnapshot.getDocuments();

//reference doc, query starts at or after this one
DocumentSnapshot indexDocSnapshot = docs.get(docs.size());

//to 'start after', or paginate, you can do below:
query.startAfter(indexDocSnapshot).limit(10).get()
                            .addOnSuccessListener(queryDocumentSnapshots -> {
                                // next 10 docs here, no extra reads necessary
                            });
Run Code Online (Sandbox Code Playgroud)