/
QueryDataSnippets.java
331 lines (299 loc) · 10.6 KB
/
QueryDataSnippets.java
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
/*
* Copyright 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.
*/
package com.example.firestore.snippets;
import com.example.firestore.snippets.model.City;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.Query.Direction;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.WriteResult;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** Snippets to support firestore querying data documentation. */
class QueryDataSnippets {
private final Firestore db;
QueryDataSnippets(Firestore db) {
this.db = db;
}
/**
* Creates cities collection and add sample documents to test queries.
*
* @return collection reference
*/
void prepareExamples() throws Exception {
// [START fs_query_create_examples]
CollectionReference cities = db.collection("cities");
List<ApiFuture<WriteResult>> futures = new ArrayList<>();
futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L,
Arrays.asList("west_coast", "norcal"))));
futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L,
Arrays.asList("west_coast", "socal"))));
futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L,
Arrays.asList("east_coast"))));
futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L,
Arrays.asList("kanto", "honshu"))));
futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L,
Arrays.asList("jingjinji", "hebei"))));
// (optional) block on documents successfully added
ApiFutures.allAsList(futures).get();
// [END fs_query_create_examples]
}
/**
* Creates a sample query.
*
* @return query
*/
Query createAQuery() throws Exception {
// [START fs_create_query]
// Create a reference to the cities collection
CollectionReference cities = db.collection("cities");
// Create a query against the collection.
Query query = cities.whereEqualTo("capital", true);
// retrieve query results asynchronously using query.get()
ApiFuture<QuerySnapshot> querySnapshot = query.get();
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
System.out.println(document.getId());
}
// [END fs_create_query]
return query;
}
/**
* Creates a sample query.
*
* @return query
*/
Query createAQueryAlternate() throws Exception {
// [START fs_create_query_country]
// Create a reference to the cities collection
CollectionReference cities = db.collection("cities");
// Create a query against the collection.
Query query = cities.whereEqualTo("state", "CA");
// retrieve query results asynchronously using query.get()
ApiFuture<QuerySnapshot> querySnapshot = query.get();
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
System.out.println(document.getId());
}
// [END fs_create_query_country]
return query;
}
/**
* Creates queries with simple where clauses.
*
* @return queries
*/
List<Query> createSimpleQueries() {
List<Query> querys = new ArrayList<>();
CollectionReference cities = db.collection("cities");
// [START fs_simple_queries]
Query countryQuery = cities.whereEqualTo("state", "CA");
Query populationQuery = cities.whereLessThan("population", 1000000L);
Query cityQuery = cities.whereGreaterThanOrEqualTo("name", "San Francisco");
// [END fs_simple_queries]
querys.add(countryQuery);
querys.add(populationQuery);
querys.add(cityQuery);
return querys;
}
/**
* Creates a query based on array containment.
*
* @return query
*/
Query createArrayQuery() {
// [START fs_array_contains_filter]
CollectionReference citiesRef = db.collection("cities");
Query westCoastQuery = citiesRef.whereArrayContains("regions", "west_coast");
// [END fs_array_contains_filter]
return westCoastQuery;
}
/**
* Creates chained where clauses.
*
* <p>Note : equality and inequality clauses over multiple fields cannot be chained.
*
* @return query
*/
Query createChainedQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_chained_query]
Query chainedQuery1 = cities.whereEqualTo("state", "CO")
.whereEqualTo("name", "Denver");
// [END fs_chained_query]
return chainedQuery1;
}
/**
* An instance of a currently unsupported chained query: equality with inequality.
* NOTE : Requires support for creation of composite indices.
*
* @return query
*/
Query createCompositeIndexChainedQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_composite_index_chained_query]
Query chainedQuery2 = cities.whereEqualTo("state", "CA")
.whereLessThan("population", 1000000L);
// [END fs_composite_index_chained_query]
return chainedQuery2;
}
/**
* An instance of a valid range/inequality query : range operators are limited to a single field.
*
* @return query
*/
Query createRangeQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_range_query]
Query validQuery1 = cities.whereGreaterThanOrEqualTo("state", "CA")
.whereLessThanOrEqualTo("state", "IN");
Query validQuery2 = cities.whereEqualTo("state", "CA")
.whereGreaterThan("population", 1000000);
// [END fs_range_query]
return validQuery1;
}
/**
* An instance of an invalid range query : range operators are limited to a single field.
*
* @return query
*/
Query createInvalidRangeQuery() {
CollectionReference cities = db.collection("cities");
// Violates constraint : range operators are limited to a single field
// [START fs_invalid_range_query]
Query invalidRangeQuery = cities.whereGreaterThanOrEqualTo("state", "CA")
.whereGreaterThan("population", 100000);
// [END fs_invalid_range_query]
return invalidRangeQuery;
}
/**
* Creates a query that combines order by with limit.
*
* @return query
*/
Query createOrderByNameWithLimitQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_order_by_name_limit_query]
Query query = cities.orderBy("name").limit(3);
// [END fs_order_by_name_limit_query]
return query;
}
/**
* Creates a query that orders by country and population(descending).
*
* @return query
*/
Query createOrderByCountryAndPopulation() {
CollectionReference cities = db.collection("cities");
// [START fs_order_by_country_population]
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
// [END fs_order_by_country_population]
return query;
}
/**
* Creates a query that combines order by in descending order with the limit operator.
*
* @return query
*/
Query createOrderByNameDescWithLimitQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_order_by_name_desc_limit_query]
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
// [END fs_order_by_name_desc_limit_query]
return query;
}
/**
* Creates a query that combines where clause with order by and limit operator.
*
* @return query
*/
Query createWhereWithOrderByAndLimitQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_where_order_by_limit_query]
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
// [END fs_where_order_by_limit_query]
return query;
}
/**
* Creates a query using a range where clause with order by. Order by must be based on the same
* field as the range clause.
*
* @return query
*/
Query createRangeWithOrderByQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_range_order_by_query]
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
// [END fs_range_order_by_query]
return query;
}
/**
* Creates an instance of an invalid range combined with order. Violates the constraint that range
* and order by are required to be on the same field.
*
* @return query
*/
Query createInvalidRangeWithOrderByQuery() {
CollectionReference cities = db.collection("cities");
// Violates the constraint that range and order by are required to be on the same field
// [START fs_invalid_range_order_by_query]
Query query = cities.whereGreaterThan("population", 2500000L).orderBy("country");
// [END fs_invalid_range_order_by_query]
return query;
}
/**
* Create a query defining the start point of a query.
*
* @return query
*/
Query createStartAtFieldQueryCursor() {
CollectionReference cities = db.collection("cities");
// [START fs_start_at_field_query_cursor]
Query query = cities.orderBy("population").startAt(4921000L);
// [END fs_start_at_field_query_cursor]
return query;
}
/**
* Create a query defining the start point of a query.
*
* @return query
*/
Query createEndAtFieldQueryCursor() {
CollectionReference cities = db.collection("cities");
// [START fs_end_at_field_query_cursor]
Query query = cities.orderBy("population").endAt(4921000L);
// [END fs_end_at_field_query_cursor]
return query;
}
/* Create queries with multiple cursor conditions. */
void createMultipleCursorConditionsQuery() {
// [START fs_multiple_cursor_conditions]
// Will return all Springfields
Query query1 = db.collection("cities")
.orderBy("name")
.orderBy("state")
.startAt("Springfield");
// Will return "Springfield, Missouri" and "Springfield, Wisconsin"
Query query2 = db.collection("cities")
.orderBy("name")
.orderBy("state")
.startAt("Springfield", "Missouri");
// [END fs_multiple_cursor_conditions]
}
}