Firestore 在 GO 中应用 Where to array

我有这些结构


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


感谢您的任何输入!


根据要求编辑 数据结构和示例数据

http://img.mukewang.com/63b3f47b00019fba20940484.jpg

牧羊人nacy
浏览 69回答 1
1回答

湖上湖

如这个答案所示:这些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"]:
打开App,查看更多内容
随时随地看视频慕课网APP