/
Program.cs
271 lines (250 loc) · 11 KB
/
Program.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright(c) 2017 Google Inc.
//
// 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
//
// http://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.
using CommandLine;
using Google.Cloud.Firestore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
namespace GoogleCloudSamples
{
public class QueryData
{
public static string Usage = @"Usage:
C:\> dotnet run command YOUR_PROJECT_ID
Where command is one of
query-create-examples
create-query-state
create-query-capital
simple-queries
chained-query
composite-index-chained-query
range-query
invalid-range-query
";
private static async Task QueryCreateExamples(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_query_create_examples]
CollectionReference citiesRef = db.Collection("cities");
await citiesRef.Document("SF").SetAsync(new Dictionary<string, object>(){
{ "Name", "San Francisco" },
{ "State", "CA" },
{ "Country", "USA" },
{ "Capital", false },
{ "Population", 860000 },
{ "Regions", new ArrayList{"west_coast", "norcal"} }
});
await citiesRef.Document("LA").SetAsync(new Dictionary<string, object>(){
{ "Name", "Los Angeles" },
{ "State", "CA" },
{ "Country", "USA" },
{ "Capital", false },
{ "Population", 3900000 },
{ "Regions", new ArrayList{"west_coast", "socal"} }
});
await citiesRef.Document("DC").SetAsync(new Dictionary<string, object>(){
{ "Name", "Washington D.C." },
{ "State", null },
{ "Country", "USA" },
{ "Capital", true },
{ "Population", 680000 },
{ "Regions", new ArrayList{"east_coast"} }
});
await citiesRef.Document("TOK").SetAsync(new Dictionary<string, object>(){
{ "Name", "Tokyo" },
{ "State", null },
{ "Country", "Japan" },
{ "Capital", true },
{ "Population", 9000000 },
{ "Regions", new ArrayList{"kanto", "honshu"} }
});
await citiesRef.Document("BJ").SetAsync(new Dictionary<string, object>(){
{ "Name", "Beijing" },
{ "State", null },
{ "Country", "China" },
{ "Capital", true },
{ "Population", 21500000 },
{ "Regions", new ArrayList{"jingjinji", "hebei"} }
});
Console.WriteLine("Added example cities data to the cities collection.");
// [END fs_query_create_examples]
}
private static async Task CreateQueryState(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_create_query_state]
CollectionReference citiesRef = db.Collection("cities");
Query query = citiesRef.WhereEqualTo("State", "CA");
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query State=CA", documentSnapshot.Id);
}
// [END fs_create_query_state]
}
private static async Task CreateQueryCapital(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
// [START fs_create_query_capital]
CollectionReference citiesRef = db.Collection("cities");
Query query = citiesRef.WhereEqualTo("Capital", true);
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query Capital=true", documentSnapshot.Id);
}
// [END fs_create_query_capital]
}
private static async Task SimpleQueries(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference citiesRef = db.Collection("cities");
// [START fs_simple_queries]
Query stateQuery = citiesRef.WhereEqualTo("State", "CA");
Query populationQuery = citiesRef.WhereGreaterThan("Population", 1000000);
Query nameQuery = citiesRef.WhereGreaterThanOrEqualTo("Name", "San Francisco");
// [END fs_simple_queries]
QuerySnapshot stateQuerySnapshot = await stateQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in stateQuerySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query State=CA", documentSnapshot.Id);
}
QuerySnapshot populationQuerySnapshot = await populationQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in populationQuerySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query Population>1000000", documentSnapshot.Id);
}
QuerySnapshot nameQuerySnapshot = await nameQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in nameQuerySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query Name>=San Francisco", documentSnapshot.Id);
}
}
private static async Task ArrayContainsQuery(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference citiesRef = db.Collection("cities");
// [START fs_array_contains_query]
Query query = citiesRef.WhereArrayContains("Regions", "west_coast");
// [END fs_array_contains_query]
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query 'Regions array_contains west_coast'", documentSnapshot.Id);
}
}
private static async Task ChainedQuery(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference citiesRef = db.Collection("cities");
// [START fs_chained_query]
Query chainedQuery = citiesRef
.WhereEqualTo("State", "CA")
.WhereEqualTo("Name", "San Francisco");
// [END fs_chained_query]
QuerySnapshot querySnapshot = await chainedQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query State=CA and Name=San Francisco", documentSnapshot.Id);
}
}
private static async Task CompositeIndexChainedQuery(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference citiesRef = db.Collection("cities");
// [START fs_composite_index_chained_query]
Query chainedQuery = citiesRef
.WhereEqualTo("State", "CA")
.WhereLessThan("Population", 1000000);
// [END fs_composite_index_chained_query]
QuerySnapshot querySnapshot = await chainedQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query State=CA and Population<1000000", documentSnapshot.Id);
}
}
private static async Task RangeQuery(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference citiesRef = db.Collection("cities");
// [START fs_range_query]
Query rangeQuery = citiesRef
.WhereGreaterThanOrEqualTo("State", "CA")
.WhereLessThanOrEqualTo("State", "IN");
// [END fs_range_query]
QuerySnapshot querySnapshot = await rangeQuery.GetSnapshotAsync();
foreach (DocumentSnapshot documentSnapshot in querySnapshot.Documents)
{
Console.WriteLine("Document {0} returned by query CA<=State<=IN", documentSnapshot.Id);
}
}
private static void InvalidRangeQuery(string project)
{
FirestoreDb db = FirestoreDb.Create(project);
CollectionReference citiesRef = db.Collection("cities");
// [START fs_invalid_range_query]
Query invalidRangeQuery = citiesRef
.WhereGreaterThanOrEqualTo("State", "CA")
.WhereGreaterThan("Population", 1000000);
// [END fs_invalid_range_query]
}
public static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Write(Usage);
return;
}
string command = args[0].ToLower();
string project = string.Join(" ",
new ArraySegment<string>(args, 1, args.Length - 1));
switch (command)
{
case "query-create-examples":
QueryCreateExamples(project).Wait();
break;
case "create-query-state":
CreateQueryState(project).Wait();
break;
case "create-query-capital":
CreateQueryCapital(project).Wait();
break;
case "simple-queries":
SimpleQueries(project).Wait();
break;
case "array-contains-query":
ArrayContainsQuery(project).Wait();
break;
case "chained-query":
ChainedQuery(project).Wait();
break;
case "composite-index-chained-query":
CompositeIndexChainedQuery(project).Wait();
break;
case "range-query":
RangeQuery(project).Wait();
break;
case "invalid-range-query":
InvalidRangeQuery(project);
break;
default:
Console.Write(Usage);
return;
}
}
}
}