Get documents from Firestore GCP using IDs inside the query with golang

Viewed 550

I'm trying to get a set of documents from my GCP Firestore collection with a query where the document id is IN an array of strings. I see it's possibile with other languages but I can't find a solution for Golang.

I'm trying the following:

Collection("myCollection").Where(firestore.DocumentID, "in", []string{"id1", "id2", "id3"}).Documents(ctx)

but I have the following error:

{
    "code": 3,
    "message": "__key__ filter value must be a Key"
}

I think it's not a best practice to save the document id as field of the document itself but it would reduce the complexity of the query and it would be a workaround. What do you think ? Any suggestion ?

1 Answers

Use Client.GetAll to get multiple documents by id.

var drs []*firestore.DocRef
for _, id := range []string{"id1", "id2", "id3"} {
    drs = append(drs, cr.Doc(id))
}
snaps, err := client.GetAll(ctx, drs)
Related