Created
December 6, 2025 08:30
-
-
Save nikolaydubina/fd590d87601ed14ee1a289fb3a7a2fdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "encoding/json" | |
| "flag" | |
| "log" | |
| "os" | |
| "cloud.google.com/go/firestore" | |
| "google.golang.org/api/iterator" | |
| ) | |
| func main() { | |
| var project, collection string | |
| flag.StringVar(&project, "project", os.Getenv("PROJECT_ID"), "GCP project ID") | |
| flag.StringVar(&collection, "collection", "", "Firestore collection") | |
| flag.Parse() | |
| if project == "" || collection == "" { | |
| flag.Usage() | |
| os.Exit(1) | |
| } | |
| ctx := context.Background() | |
| db, err := firestore.NewClient(ctx, project) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer db.Close() | |
| enc := json.NewEncoder(os.Stdout) | |
| docs := db.Collection(collection).Documents(ctx) | |
| defer docs.Stop() | |
| for { | |
| doc, err := docs.Next() | |
| if err == iterator.Done { | |
| break | |
| } | |
| if err != nil { | |
| log.Printf("error: %s", err) | |
| continue | |
| } | |
| if err := enc.Encode(doc.Data()); err != nil { | |
| log.Printf("error encoding doc %s: %s", doc.Ref.ID, err) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment