我有这些结构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 关注
- 66 浏览
添加回答
举报
0/150
提交
取消