I was struggling to find this for the Golang Firebase SDK but finally got it. Hope this helps somebody out there!
package main
import (
"context"
"fmt"
"log"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go/v4"
"google.golang.org/api/option"
)
type (
Car struct {
ID string
Name string `firestore:"name"`
Make string `firestore:"make"`
Price float64 `firestore:"make"`
}
)
func main() {
ctx := context.Background()
// Use a service account
options := option.WithCredentialsFile("PATH/TO/SERVICE/FILE.json")
// Set project id
conf := &firebase.Config{ProjectID: "PROJECT_NAME"}
// Initialize app
app, err := firebase.NewApp(ctx, conf, options)
if err != nil {
log.Fatal(err)
}
// Get firestore client
client, err := app.Firestore(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Close()
collectionRef := client.Collection("CAR_COLLECTION")
// Create docment list of documents from "CAR_COLLECTION"
var skipDocs []*firestore.DocumentRef
idList := []string{"001", "002", "003"}
for _, id := range idList {
skipDocs = append(skipDocs, collectionRef.Doc(id))
}
// firestore.DocumentID == "__name__"
docs, err := collectionRef.Where(firestore.DocumentID, "not-in", skipDocs).Documents(ctx).GetAll()
if err != nil {
log.Fatal(err)
}
var carList []Car
for _, doc := range docs {
var car Car
// Unmarshall item
doc.DataTo(&car)
car.ID = doc.Ref.ID
// Add car to list
carList = append(carList, car)
}
// Print car list
fmt.Println(carList)
}