[英]Firestore apply Where to array in 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
.我正在尝试查询我的 Firestore 数据库以获取任何具有特定
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.总是返回 null。
Using nodejs I could also use使用 nodejs 我也可以使用
client.Collection("notifications").where("Content", "array-contains", { Language: "en" })
but I have no idea how to translate to GO但我不知道如何翻译成 GO
Thanks for any input!感谢您的任何意见!
EDIT Data structure and sample data as requested根据要求编辑数据结构和示例数据
As shown in this answer :如本答案所示:
The
array-contains
operations check if an array contains a specific (complete) value.array-contains
操作检查数组是否包含特定(完整)值。 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.将不返回任何值,因为
array-contains
过滤器正在寻找整个 object。 But a similar query (workaround) is possible as shown on this other answer :但是类似的查询(解决方法)是可能的,如this other answer所示:
However, there is a possible workaround: it is actually possible to query for the entire object
但是,有一个可能的解决方法:实际上可以查询整个 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"]
.例如:
partyMemberNames: ["John Travolta", "Olivia Newton"]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.