簡體   English   中英

Firestore 跨多個字段查詢相同的值

[英]Firestore Query Across Multiple Fields for Same Value

是否可以查詢 firestore 文檔中的多個字段以獲得相同的值?

想象一下,我有很多文件,每份文件都描述了一項運動:

sportsRef.collection("sports").documents [
["bowling" : [
"equipment": "bowling ball, bowling alley, bowling shoes",
"description": "bowl the ball" ]]

["football": [
"equipment": "ball, boots, shin pads, goalie gloves",
"description": "kick the ball in the goal" ]]

["darts": [
"equipment": "darts, dartboard",
"description": "throw the dart at the board" ]]

["boxing": [
"equipment": "boxing gloves, ring",
"description": "punch your contender" ]]

["shot put": [
"equipment": "shot put",
"description": "throw the shot put, looks like a ball" ]]
]

如果我搜索“ball”的值,是否有任何方法可以查詢,將返回所有在其值中包含“ball”的文檔。 所以在這種情況下,我會返回保齡球、足球和鉛球(“球”在鉛球的描述中)。

我問的原因是因為我在我的 iOS 應用程序中實現了一個 UISearchController 並試圖通過在我的應用程序中構建查詢來在我的 Firestore 中進行搜索。

Firestore 不支持子字符串、正則表達式或全文搜索類型的查詢。 這些類型的搜索不會隨着 Firestore 管理其索引的方式而擴展。

如果您需要全文本搜索,請考慮將您的數據復制到搜索引擎中,例如 Algolia。 Firebase 文檔中討論了這種類型的解決方案。

做這樣的事情的一種方法是像這樣重組你的數據:

["bowling" : [
"ball": true, "bowling alley": true, "bowling shoes": true,
"description": "bowl the ball" ]]

然后查詢 "ball" 等於true的文檔:

Firestore.firestore().collection("sports").whereField("ball", isEqualTo: true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM