英文:
Firestore apply Where to array in GO
问题
我有以下这些结构体:
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"})
始终返回null。
在Node.js中,我还可以使用以下代码:
client.Collection("notifications").where("Content", "array-contains", { Language: "en" })
但是我不知道如何将其转换为Go语言。
谢谢您的任何帮助!
编辑
如要求所示,以下是数据结构和示例数据:
英文:
I have these structs
type Notification struct {
Content []NotificationContent `json:"content"`
CreatedAt time.Time `json:"createdAt"`
}
type NotificationContent struct {
Language string `json:"language"`
Title string `json:"title"`
}
And I'm trying to query my Firestore database to fetch any notification that have a specific Language
.
Using
query := client.Collection("notifications").Where("Content.Language", "==", "en")
or
query := client.Collection("notifications").Where("Content.Language", "in", [1]string{"en"})
always return null.
Using nodejs I could also use
client.Collection("notifications").where("Content", "array-contains", { Language: "en" })
but I have no idea how to translate to GO
Thanks for any input!
EDIT
Data structure and sample data as requested
答案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"]
。
英文:
As shown in this answer:
> The array-contains
operations check if an array contains a specific (complete) value. It can't check if an array of objects contains an item with a specific value for a property.
Therefore, that is why the query you are trying to do won't work.
From the test I have made, the following query
query := client.Collection("notifications").Where("Content", "array-contains", map[string]string{"Language":"en"}).Documents(ctx)
will return no values, as the array-contains
filter is looking for an entire object. But a similar query (workaround) is possible as shown on this other answer:
> However, there is a possible workaround: it is actually possible to query for the entire object
In your case, the query would be as follows:
query := client.Collection("notifications").Where("Content", "array-contains", map[string]string{"Language":"en", "Title":"Foo Bar"}).Documents(ctx)
As you can see in the question, the data structure is similar to yours and the case of use is the same as yours. So, the recommendation made in this answer would fit your problem:
> The only way to do your query, is to add an additional field to your document with just the value you want to query existence on. So for example: partyMemberNames: ["John Travolta", "Olivia Newton"]
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论