英文:
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")
英文:
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")
答案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'd with each other. Therefore, it doesn'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 "exp" **OR** BUCKET_1_KEY is equals to "options", 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论