【问题标题】:How to query the document which contains the given values in Firestore document? [duplicate]如何在 Firestore 文档中查询包含给定值的文档? [复制]
【发布时间】:2021-09-10 20:56:17
【问题描述】:
我的 Firestore 结构如图所示
我的 Firestore 文档有很多文档,其中一个如图所示。在我的 Android 应用程序中,我想查询所有具有“3P”、“紧急”和“任务挑战”值的文档。这意味着如果一个文档有“3P”、“紧急”和“任务挑战”,那么这个文档应该是我的查询结果。如果文档只有“3P”但没有其他值(如“紧急”、“任务挑战”),那么它不应该出现在我的查询结果中。如何在 Firestore 中执行此操作。
【问题讨论】:
标签:
android
firebase
google-cloud-firestore
【解决方案1】:
我认为目前还不能在 Firestore 中执行类似 'array-contains-all' 的操作。 @Doug 有一个类似情况的 answer。
每个查询最多可以使用一个数组包含子句。
目前最好的方法是使用地图并使用多个whereEqualTo 方法:
db.collection("dungeon")
.whereEqualTo("textMap.3P", true)
.whereEqualTo("textMap.urgent", true)
.whereEqualTo("textMap.mission_challenge", true)
.get()
文件:
{
...otherFields,
textMap: {
"urgent": true,
...otherMapFields
}
}