Firestore 查询 - 数组包含所有

Dam*_*ala 6 javascript firebase google-cloud-firestore

我的用例涉及使用“array-contains-all”过滤 Firestore 文档,该文档的名称是我为了解决此问题而编造的。然而,'array-contains-any'已经存在,但它不检查数组中是否存在所有元素,而是检查any。我很难找到内置的解决方案或找到更好的方法来实现相同的结果,而不是查询所有文档(昂贵),然后在最终数组传递给客户端之前在云函数中过滤结果。

举个例子,我们想知道哪些住宿网站拥有我们感兴趣并希望查询的所有以下设施:

[
    'lockable_bedroom_door',
    'private_bathroom',
    'internet',
    'desk',
    'safe_place_to_store_valuables'
]
Run Code Online (Sandbox Code Playgroud)

在所有 13 个可用设施的数组中:

[
    'kettle',
    'microwave',
    'cooker',
    'washing_machine',
    'fully_functional_kitchen',
    'lockable_bedroom_door',
    'private_bathroom',
    'shared_bathroom',
    'internet',
    'desk',
    'common_room_lounge',
    'safe_place_to_store_valuables',
    'free_on-site_parking'
]
Run Code Online (Sandbox Code Playgroud)

如何在牢记 Firestore 限制和用户可能选择的设施数量的情况下实现这一目标?

Dou*_*son 6

只要您像现在一样使用列表类型字段,就不会找到一种高效的查询方法。如果将数据复制到新的地图类型字段中,这是可能的。

facilities: {
    'kettle': true
    'microwave': true
    'cooker': true
}
Run Code Online (Sandbox Code Playgroud)

使用包含已知布尔值的映射,您可以像这样查询:

firestore
    .collection("your-collection")
    .where("facilities.kettle", "==", true)
    .where("facilities.microwave", "==", true)
    .where("facilities.cooker", "==", true)
Run Code Online (Sandbox Code Playgroud)

  • 这将需要设施下每个键的索引......来吧火力......你可以做得更好! (6认同)