英文:
firestore paginations error: order by clause cannot contain a field with an equality filter
问题
Firestore在尝试使用start_after进行查询分页时出现错误。
我的代码中的一个简短摘录:
query = USER_COLLECTION.where("status", "==", "active")
query = query.where("favFruits", "in", ["mango", "orange"])
query = query.order_by("name", "DESCENDING")
如果此时执行query.get(),将会返回有效的结果,但以下步骤会失败:
query = query.start_after(USER_COLLECTION.document(last_processed_document_id).get())
在上述query上调用.get()会导致错误:
google.api_core.exceptions.InvalidArgument: 400 order by clause cannot contain a field with an equality filter favFruits
英文:
Firestore gives an error when trying to paginate the query using start_after.
A short excerpt from my code:
query = USER_COLLECTION.where("status", "==", "active")
query = query.where("favFruits", "in", ["mango", "orange"])
query = query.order_by("name", "DESCENDING")
If I do query.get() at this point, it gives me valid results but the below step fails
query = query.start_after(USER_COLLECTION.document(last_processed_document_id).get())
calling .get() on the above query is gives an error
google.api_core.exceptions.InvalidArgument: 400 order by clause cannot contain a field with an equality filter favFruits
答案1
得分: 1
根据您提供的示例查询,这是使用Cloud Firestore进行数据排序和限制的一部分限制。
根据此文档:
orderBy()子句还会过滤给定字段的存在。结果集不会包括不包含这些字段的文档。- 如果您使用范围比较 (
<,<=,>,>=) 进行过滤,您的第一个排序必须基于相同的字段。 - 您不能按包含在相等 (
=) 或in子句中的任何字段对查询进行排序。
这意味着您的查询应该如下所示:
query = query.where("favFruits", "in", ["mango", "orange"])
query = query.order_by("favFruits", "DESCENDING")
英文:
Based on your sample query that you provided, this is a part of the limitation of ordering and limiting data with Cloud Firestore.
According to this documentation:
> - An orderBy() clause also filters for existence of the given fields. The result set will not include documents that do not contain the given fields.
> - If you include a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field.
> - You cannot order your query by any field included in an equality (=) or in clause.
Meaning your query should appear like this:
query = query.where("favFruits", "in", ["mango", "orange"])
query = query.order_by("favFruits", "DESCENDING")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论