-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(firestore): pipeline queries snippets #5506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d1fba0f
feat(firestore): pipeline queries snippets
bhshkh bfca916
fix region tags and copyright year
bhshkh 3f8fba9
write error to io.Writer
bhshkh 0de2fdc
Merge branch 'main' into feature/fspq
bhshkh f7119fc
add dml and search samples
bhshkh 5637442
refactor into separate files
bhshkh 3bc36a6
Merge branch 'main' into feature/fspq
grayside File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
| 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 | ||
| } |
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
| 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 | ||
| } | ||
| 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.