1

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

Data structure and sample data

Valerio
  • 3,297
  • 3
  • 27
  • 44

1 Answers1

1

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"].

Rogelio Monter
  • 1,084
  • 7
  • 18