我有這些結構type Notification struct { Content []NotificationContent `json:"content"` CreatedAt time.Time `json:"createdAt"`}type NotificationContent struct { Language string `json:"language"` Title string `json:"title"`}我正在嘗試查詢我的 Firestore 數據庫以獲取任何具有特定Language.使用query := client.Collection("notifications").Where("Content.Language", "==", "en")要么query := client.Collection("notifications").Where("Content.Language", "in", [1]string{"en"})總是返回空值。使用 nodejs 我也可以使用client.Collection("notifications").where("Content", "array-contains", { Language: "en" })但我不知道如何翻譯成 GO感謝您的任何輸入!根據要求編輯 數據結構和示例數據
1 回答

湖上湖
TA貢獻2003條經驗 獲得超2個贊
如這個答案所示:
這些
array-contains
操作檢查數組是否包含特定(完整)值。它無法檢查對象數組是否包含具有特定屬性值的項目。
因此,這就是您嘗試執行的查詢不起作用的原因。
根據我所做的測試,以下查詢
query := client.Collection("notifications").Where("Content", "array-contains", map[string]string{"Language":"en"}).Documents(ctx)
將不返回任何值,因為array-contains
過濾器正在尋找整個對象。但是如其他答案所示,類似的查詢(解決方法)是可能的:
但是,有一個可能的解決方法:實際上可以查詢整個對象
在您的情況下,查詢將如下所示:
query := client.Collection("notifications").Where("Content", "array-contains", map[string]string{"Language":"en", "Title":"Foo Bar"}).Documents(ctx)
正如您在問題中看到的那樣,數據結構與您的類似,使用情況與您相同。因此,此答案中的建議將適合您的問題:
進行查詢的唯一方法是在文檔中添加一個附加字段,其中僅包含您要查詢存在的值。例如
partyMemberNames: ["John Travolta", "Olivia Newton"]
:
- 1 回答
- 0 關注
- 100 瀏覽
添加回答
舉報
0/150
提交
取消