/
query_data.rb
444 lines (400 loc) · 14.6 KB
/
query_data.rb
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# Copyright 2020 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.
require "google/cloud/firestore"
def query_create_examples project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_dataset]
# [START fs_query_create_examples]
cities_ref = firestore.col collection_path
cities_ref.doc("SF").set(
{
name: "San Francisco",
state: "CA",
country: "USA",
capital: false,
population: 860_000,
regions: ["west_coast", "norcal"]
}
)
cities_ref.doc("LA").set(
{
name: "Los Angeles",
state: "CA",
country: "USA",
capital: false,
population: 3_900_000,
regions: ["west_coast", "socal"]
}
)
cities_ref.doc("DC").set(
{
name: "Washington D.C.",
state: nil,
country: "USA",
capital: true,
population: 680_000,
regions: ["east_coast"]
}
)
cities_ref.doc("TOK").set(
{
name: "Tokyo",
state: nil,
country: "Japan",
capital: true,
population: 9_000_000,
regions: ["kanto", "honshu"]
}
)
cities_ref.doc("BJ").set(
{
name: "Beijing",
state: nil,
country: "China",
capital: true,
population: 21_500_000,
regions: ["jingjinji", "hebei"]
}
)
# [END fs_query_create_examples]
# [END firestore_query_filter_dataset]
puts "Added example cities data to the cities collection."
end
def create_query_state project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_eq_string]
# [START fs_create_query_state]
cities_ref = firestore.col collection_path
query = cities_ref.where "state", "=", "CA"
query.get do |city|
puts "Document #{city.document_id} returned by query state=CA."
end
# [END fs_create_query_state]
# [END firestore_query_filter_eq_string]
end
def create_query_capital project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_eq_boolean]
# [START fs_create_query_capital]
cities_ref = firestore.col collection_path
query = cities_ref.where "capital", "=", true
query.get do |city|
puts "Document #{city.document_id} returned by query capital=true."
end
# [END fs_create_query_capital]
# [END firestore_query_filter_eq_boolean]
end
def simple_queries project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
cities_ref = firestore.col collection_path
# [START firestore_query_filter_single_examples]
# [START fs_simple_queries]
state_query = cities_ref.where "state", "=", "CA"
population_query = cities_ref.where "population", ">", 1_000_000
name_query = cities_ref.where "name", ">=", "San Francisco"
# [END fs_simple_queries]
# [END firestore_query_filter_single_examples]
state_query.get do |city|
puts "Document #{city.document_id} returned by query state=CA."
end
population_query.get do |city|
puts "Document #{city.document_id} returned by query population>1000000."
end
name_query.get do |city|
puts "Document #{city.document_id} returned by query name>=San Francisco."
end
end
def chained_query project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
cities_ref = firestore.col collection_path
# [START firestore_query_filter_compound_multi_eq]
# [START fs_chained_query]
chained_query = cities_ref.where("state", "=", "CA").where("name", "=", "San Francisco")
# [END fs_chained_query]
# [END firestore_query_filter_compound_multi_eq]
chained_query.get do |city|
puts "Document #{city.document_id} returned by query state=CA and name=San Francisco."
end
end
def composite_index_chained_query project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
cities_ref = firestore.col collection_path
# [START firestore_query_filter_compound_multi_eq_lt]
# [START fs_composite_index_chained_query]
chained_query = cities_ref.where("state", "=", "CA").where("population", "<", 1_000_000)
# [END fs_composite_index_chained_query]
# [END firestore_query_filter_compound_multi_eq_lt]
chained_query.get do |city|
puts "Document #{city.document_id} returned by query state=CA and population<1000000."
end
end
def range_query project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
cities_ref = firestore.col collection_path
# [START firestore_query_filter_range_valid]
# [START fs_range_query]
range_query = cities_ref.where("state", ">=", "CA").where("state", "<=", "IN")
# [END fs_range_query]
# [END firestore_query_filter_range_valid]
range_query.get do |city|
puts "Document #{city.document_id} returned by query CA<=state<=IN."
end
end
def invalid_range_query project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
cities_ref = firestore.col collection_path
# [START firestore_query_filter_range_invalid]
# [START fs_invalid_range_query]
invalid_range_query = cities_ref.where("state", ">=", "CA").where("population", ">", 1_000_000)
# [END fs_invalid_range_query]
# [END firestore_query_filter_range_invalid]
end
def in_query_without_array project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_in]
# [START fs_query_filter_in]
cities_ref = firestore.col collection_path
usr_or_japan = cities_ref.where "country", "in", ["USA", "Japan"]
# [END fs_query_filter_in]
# [END firestore_query_filter_in]
usr_or_japan.get do |city|
puts "Document #{city.document_id} returned by query in ['USA','Japan']."
end
end
def in_query_with_array project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_in_with_array]
# [START fs_query_filter_in_with_array]
cities_ref = firestore.col collection_path
exactly_one_cost = cities_ref.where "regions", "in", [["west_coast"], ["east_coast"]]
# [END fs_query_filter_in_with_array]
# [END firestore_query_filter_in_with_array]
exactly_one_cost.get do |city|
puts "Document #{city.document_id} returned by query in [['west_coast'], ['east_coast']]."
end
end
def query_not_equals project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_not_eq]
# [START fs_query_not_equals]
cities_ref = firestore.col collection_path
query = cities_ref.where "capital", "!=", false
# [END fs_query_not_equals]
# [END firestore_query_filter_not_eq]
query.get do |city|
puts "Document #{city.document_id} returned by query capital!=false."
end
end
def filter_not_in project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_not_in]
# [START fs_filter_not_in]
cities_ref = firestore.col collection_path
usr_or_japan = cities_ref.where "country", "not_in", ["USA", "Japan"]
# [END fs_filter_not_in]
# [END firestore_query_filter_not_in]
usr_or_japan.get do |city|
puts "Document #{city.document_id} returned by query not_in ['USA','Japan']."
end
end
def array_contains_any_queries project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_array_contains_any]
# [START fs_query_filter_array_contains_any]
cities_ref = firestore.col collection_path
costal_cities = cities_ref.where "regions", "array-contains-any", ["west_coast", "east_coast"]
# [END fs_query_filter_array_contains_any]
# [END firestore_query_filter_array_contains_any]
costal_cities.get do |city|
puts "Document #{city.document_id} returned by query array-contains-any ['west_coast', 'east_coast']."
end
end
def array_contains_filter project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_filter_array_contains]
# [START fs_array_contains_filter]
cities_ref = firestore.col collection_path
cities = cities_ref.where "regions", "array-contains", "west_coast"
# [END fs_array_contains_filter]
# [END firestore_query_filter_array_contains]
cities.get do |city|
puts "Document #{city.document_id} returned by query array-contains 'west_coast'."
end
end
def collection_group_query project_id:, collection_path: "cities"
# project_id = "Your Google Cloud Project ID"
# collection_path = "cities"
firestore = Google::Cloud::Firestore.new project_id: project_id
# [START firestore_query_collection_group_dataset]
# [START fs_collection_group_query_data_setup]
cities_ref = firestore.col collection_path
sf_landmarks = cities_ref.document("SF").collection("landmarks")
sf_landmarks.document.set(
{
name: "Golden Gate Bridge",
type: "bridge"
}
)
sf_landmarks.document.set(
{
name: "Legion of Honor",
type: "museum"
}
)
la_landmarks = cities_ref.document("LA").collection("landmarks")
la_landmarks.document.set(
{
name: "Griffith Park",
type: "park"
}
)
la_landmarks.document.set(
{
name: "The Getty",
type: "museum"
}
)
dc_landmarks = cities_ref.document("DC").collection("landmarks")
dc_landmarks.document.set(
{
name: "Lincoln Memorial",
type: "memorial"
}
)
dc_landmarks.document.set(
{
name: "National Air and Space Museum",
type: "museum"
}
)
tok_landmarks = cities_ref.document("TOK").collection("landmarks")
tok_landmarks.document.set(
{
name: "Ueno Park",
type: "park"
}
)
tok_landmarks.document.set(
{
name: "National Museum of Nature and Science",
type: "museum"
}
)
bj_landmarks = cities_ref.document("BJ").collection("landmarks")
bj_landmarks.document.set(
{
name: "Jingshan Park",
type: "park"
}
)
bj_landmarks.document.set(
{
name: "Beijing Ancient Observatory",
type: "museum"
}
)
# [END fs_collection_group_query_data_setup]
# [END firestore_query_collection_group_dataset]
# [START firestore_query_collection_group_filter_eq]
# [START fs_collection_group_query]
museums = firestore.collection_group("landmarks").where("type", "==", "museum")
museums.get do |museum|
puts "#{museum[:type]} name is #{museum[:name]}."
end
# [END fs_collection_group_query]
# [END firestore_query_collection_group_filter_eq]
end
if $PROGRAM_NAME == __FILE__
project = ENV["FIRESTORE_PROJECT"]
case ARGV.shift
when "query_create_examples"
query_create_examples project_id: project
when "create_query_state"
create_query_state project_id: project
when "create_query_capital"
create_query_capital project_id: project
when "simple_queries"
simple_queries project_id: project
when "chained_query"
chained_query project_id: project
when "composite_index_chained_query"
composite_index_chained_query project_id: project
when "range_query"
range_query project_id: project
when "invalid_range_query"
invalid_range_query project_id: project
when "in_query_without_array"
in_query_without_array project_id: project
when "in_query_with_array"
in_query_with_array project_id: project
when "query_not_equals"
query_not_equals project_id: project
when "filter_not_in"
filter_not_in project_id: project
when "array_contains_any_queries"
array_contains_any_queries project_id: project
when "array_contains_filter"
array_contains_filter project_id: project
when "collection_group_query"
collection_group_query project_id: project
else
puts <<~USAGE
Usage: bundle exec ruby query_data.rb [command]
Commands:
query_create_examples Create an example collection of documents.
create_query_state Create a query by state.
create_query_capital Create a query by capital.
simple_queries Create simple queries with a single where clause.
chained_query Create a query with chained clauses.
composite_index_chained_query Create a composite index chained query.
range_query Create a query with range clauses.
invalid_range_query An example of an invalid range query.
in_query_without_array In queries without array.
in_query_with_array In queries with array.
query_not_equals Create a query with a NOT_EQUAL where clause.
filter_not_in Create a query with a NOT_IN where clause.
array_contains_any_queries Array contains any in query.
array_contains_filter Array contains filter.
collection_group_query Add sub collection and filter.
USAGE
end
end