-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
collection_group_query.go
116 lines (96 loc) · 3.19 KB
/
collection_group_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2019 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
// [START firestore_query_collection_group_filter_eq]
import (
"context"
"fmt"
"io"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
// collectionGroupQuery runs a collection group query over the data created by
// collectionGroupSetup.
func collectionGroupQuery(w io.Writer, projectID string) error {
ctx := context.Background()
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
return fmt.Errorf("firestore.NewClient: %w", err)
}
defer client.Close()
it := client.CollectionGroup("landmarks").Where("type", "==", "museum").Documents(ctx)
for {
doc, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return fmt.Errorf("documents iterator: %w", err)
}
fmt.Fprintf(w, "%s: %s", doc.Ref.ID, doc.Data()["name"])
}
return nil
}
// [END firestore_query_collection_group_filter_eq]
func partitionQuery(ctx context.Context, client *firestore.Client) error {
// [START firestore_partition_query]
cities := client.CollectionGroup("cities")
// Get a partioned query for the cities collection group, with a maximum
// partition count of 10
partitionedQueries, err := cities.GetPartitionedQueries(ctx, 10)
if err != nil {
return fmt.Errorf("GetPartitionedQueries: %w", err)
}
fmt.Printf("Collection Group query partitioned to %d queries\n", len(partitionedQueries))
// Retrieve the first query and iterate over the documents contained.
query := partitionedQueries[0]
iter := query.Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return fmt.Errorf("documents iterator: %w", err)
}
fmt.Println(doc.Data())
}
// [END firestore_partition_query]
return nil
}
func serializePartitionQuery(ctx context.Context, client *firestore.Client) error {
// [START firestore_partition_query_serialization]
cities := client.CollectionGroup("cities")
// Get a partioned query for the cities collection group, with a maximum
// partition count of 10
partitionedQueries, err := cities.GetPartitionedQueries(ctx, 10)
if err != nil {
return err
}
fmt.Printf("Collection Group query partitioned to %d queries\n", len(partitionedQueries))
query := partitionedQueries[0]
// Serialize a query created by GetPartitionedQueries
bytes, err := query.Serialize()
if err != nil {
return fmt.Errorf("Serialize: %w", err)
}
// Deserialize a query created by Query.Serialize
deserializedQuery, err := client.CollectionGroup("").Deserialize(bytes)
if err != nil {
return fmt.Errorf("Deserialize: %w", err)
}
// [END firestore_partition_query_serialization]
_ = deserializedQuery
return nil
}