可以在相同字段中两次使用WhereEqualTo吗?

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

Can I use WhereEqualTo two time in the same field?

问题

我想按照BUCKET_1_KEY字段中的两个数据进行排序,但在我的情况下无法正常工作。这是否可能?

db.collection(XPERT_MASTER_KEY)
                .document(xpertId)
                .collection(RESPONSES_KEY)
                .whereEqualTo(BUCKET_1_KEY, "exp")
                .whereEqualTo(BUCKET_1_KEY, "option")

可以在相同字段中两次使用WhereEqualTo吗?

英文:

I want to sort data in BUCKET_1_KEY field by two data, but it's not working in my case. Is it possible?

db.collection(XPERT_MASTER_KEY)
                .document(xpertId)
                .collection(RESPONSES_KEY)
                .whereEqualTo(BUCKET_1_KEY, "exp")
                .whereEqualTo(BUCKET_1_KEY, "option")

可以在相同字段中两次使用WhereEqualTo吗?

答案1

得分: 4

Firestore查询中的条件逻辑上与其他条件AND连接。因此,在同一字段的单个查询中具有两个相等条件是没有意义的。一个字段值不可能同时等于两个不同的字符串。

如果您尝试实现逻辑OR查询,尝试获取所有BUCKET_1_KEY等于"exp" BUCKET_1_KEY等于"options"的文档,您应该使用IN查询。请阅读关于IN查询的文档。如果您尝试使用IN查询执行逻辑OR,代码将如下所示:

db.collection(XPERT_MASTER_KEY)
                .document

<details>
<summary>英文:</summary>

Conditions in Firestore queries are logically AND&#39;d with each other.  Therefore, it doesn&#39;t make sense to have two equals conditions in a single query on the same field.  A field value cannot possibly be equal to two different strings at the same time.

If you are trying to implement a logical OR query, trying to get all documents where BUCKET_1_KEY is equal to &quot;exp&quot; **OR** BUCKET_1_KEY is equals to &quot;options&quot;, you should be using a IN query instead.  Please [read the documentation about IN queries][1].  If you are trying to perform a logical OR with an IN query, it would look like this:

db.collection(XPERT_MASTER_KEY)
.document(xpertId)
.collection(RESPONSES_KEY)
.whereIn(BUCKET_1_KEY, Arrays.asList("exp", "option"))


  [1]: https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any

</details>



huangapple
  • 本文由 发表于 2020年1月3日 14:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574093.html
匿名

发表评论

匿名网友

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

确定