Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
411 changes: 411 additions & 0 deletions firestore/pipeline_snippets_aggregate.go

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions firestore/pipeline_snippets_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package firestore

import (
"context"
"fmt"
"io"

"cloud.google.com/go/firestore"
)

func arrayConcatFunction(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_array_concat]
snapshot := client.Pipeline().
Collection("books").
Select(firestore.Fields(
firestore.ArrayConcat(firestore.FieldOf("genre"), firestore.FieldOf("subGenre")).As("allGenres"),
)).
Execute(ctx)
// [END firestore_array_concat]
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
fmt.Fprintln(w, results)
return nil
}

func arrayContainsFunction(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_array_contains]
snapshot := client.Pipeline().
Collection("books").
Select(firestore.Fields(
firestore.ArrayContains(firestore.FieldOf("genre"), "mystery").As("isMystery"),
)).
Execute(ctx)
// [END firestore_array_contains]
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
fmt.Fprintln(w, results)
return nil
}

func arrayContainsAllFunction(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_array_contains_all]
snapshot := client.Pipeline().
Collection("books").
Select(firestore.Fields(
firestore.ArrayContainsAll(firestore.FieldOf("genre"), []string{"fantasy", "adventure"}).As("isFantasyAdventure"),
)).
Execute(ctx)
// [END firestore_array_contains_all]
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
fmt.Fprintln(w, results)
return nil
}

func arrayContainsAnyFunction(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_array_contains_any]
snapshot := client.Pipeline().
Collection("books").
Select(firestore.Fields(
firestore.ArrayContainsAny(firestore.FieldOf("genre"), []string{"fantasy", "nonfiction"}).As("isMysteryOrFantasy"),
)).
Execute(ctx)
// [END firestore_array_contains_any]
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
fmt.Fprintln(w, results)
return nil
}

func arrayLengthFunction(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_array_length]
snapshot := client.Pipeline().
Collection("books").
Select(firestore.Fields(
firestore.ArrayLength(firestore.FieldOf("genre")).As("genreCount"),
)).
Execute(ctx)
// [END firestore_array_length]
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
fmt.Fprintln(w, results)
return nil
}

func arrayReverseFunction(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_array_reverse]
snapshot := client.Pipeline().
Collection("books").
Select(firestore.Fields(
firestore.ArrayReverse(firestore.FieldOf("genre")).As("reversedGenres"),
)).
Execute(ctx)
// [END firestore_array_reverse]
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
fmt.Fprintln(w, results)
return nil
}
127 changes: 127 additions & 0 deletions firestore/pipeline_snippets_concepts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package firestore

import (
"context"
"fmt"
"io"

"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)

func pipelineConcepts(w io.Writer, client *firestore.Client) error {
// [START firestore_pipeline_concepts]
pipeline := client.Pipeline().
Collection("cities").
Where(firestore.FieldOf("population").GreaterThan(100000)).
Sort(firestore.Orders(firestore.Ascending(firestore.FieldOf("name")))).
Limit(10)
// [END firestore_pipeline_concepts]
fmt.Fprintln(w, pipeline)
return nil
}

func basicRead(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_basic_read]
pipeline := client.Pipeline().Collection("users")
snapshot := pipeline.Execute(ctx)
results, err := snapshot.Results().GetAll()
if err != nil {
return err
}
for _, result := range results {
fmt.Fprintf(w, "%s => %v\n", result.Ref().ID, result.Data())
}
// or, one at a time
it := pipeline.Execute(ctx).Results()
for {
result, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
fmt.Fprintf(w, "%s => %v\n", result.Ref().ID, result.Data())
}
// [END firestore_basic_read]
return nil
}

func pipelineInitialization(w io.Writer, projectID string) error {
ctx := context.Background()
// [START firestore_pipeline_initialization]
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
return err
}
Comment thread
bhshkh marked this conversation as resolved.
Outdated
pipeline := client.Pipeline().Collection("books")
// [END firestore_pipeline_initialization]
fmt.Fprintln(w, pipeline)
defer client.Close()
return nil
}

func fieldVsConstants(w io.Writer, client *firestore.Client) error {
// [START firestore_field_or_constant]
pipeline := client.Pipeline().Collection("cities").
Where(firestore.FieldOf("name").Equal(firestore.ConstantOf("Toronto")))
// [END firestore_field_or_constant]
fmt.Fprintln(w, pipeline)
return nil
}

func inputStages(w io.Writer, client *firestore.Client) error {
ctx := context.Background()
// [START firestore_input_stages]
// Return all restaurants in San Francisco
results1, err := client.Pipeline().Collection("cities/sf/restaurants").Execute(ctx).Results().GetAll()
if err != nil {
return err
}

// Return all restaurants
results2, err := client.Pipeline().CollectionGroup("restaurants").Execute(ctx).Results().GetAll()
if err != nil {
return err
}

// Return all documents across all collections in the database (the entire database)
results3, err := client.Pipeline().Database().Execute(ctx).Results().GetAll()
if err != nil {
return err
}

// Batch read of 3 documents
results4, err := client.Pipeline().
Documents([]*firestore.DocumentRef{
client.Collection("cities").Doc("SF"),
client.Collection("cities").Doc("DC"),
client.Collection("cities").Doc("NY"),
}).
Execute(ctx).Results().GetAll()
if err != nil {
return err
}
// [END firestore_input_stages]
fmt.Fprintln(w, results1)
fmt.Fprintln(w, results2)
fmt.Fprintln(w, results3)
fmt.Fprintln(w, results4)
return nil
}
Loading
Loading