Firestore分页错误:orderBy子句不能包含带有相等筛选器的字段

huangapple go评论131阅读模式
英文:

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 (&lt;, &lt;=, &gt;, &gt;=), 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(&quot;favFruits&quot;, &quot;in&quot;, [&quot;mango&quot;, &quot;orange&quot;])
query = query.order_by(&quot;favFruits&quot;, &quot;DESCENDING&quot;)

huangapple
  • 本文由 发表于 2023年3月7日 01:21:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定