|
17 | 17 | package com.example.firestore.snippets; |
18 | 18 |
|
19 | 19 | import com.example.firestore.snippets.model.City; |
20 | | - |
21 | 20 | import com.google.api.core.ApiFuture; |
22 | 21 | import com.google.api.core.ApiFutures; |
23 | 22 | import com.google.cloud.firestore.CollectionReference; |
24 | | -import com.google.cloud.firestore.DocumentReference; |
25 | 23 | import com.google.cloud.firestore.DocumentSnapshot; |
26 | 24 | import com.google.cloud.firestore.Firestore; |
27 | 25 | import com.google.cloud.firestore.Query; |
28 | 26 | import com.google.cloud.firestore.Query.Direction; |
29 | 27 | import com.google.cloud.firestore.QueryDocumentSnapshot; |
30 | 28 | import com.google.cloud.firestore.QuerySnapshot; |
31 | 29 | import com.google.cloud.firestore.WriteResult; |
32 | | - |
33 | | -import com.google.firestore.v1beta1.Document; |
34 | | -import com.google.protobuf.Api; |
35 | 30 | import java.util.ArrayList; |
36 | 31 | import java.util.Arrays; |
| 32 | +import java.util.HashMap; |
37 | 33 | import java.util.List; |
38 | 34 | import java.util.concurrent.ExecutionException; |
39 | 35 | import java.util.concurrent.TimeUnit; |
@@ -383,4 +379,66 @@ List<Query> paginateCursor() throws InterruptedException, ExecutionException, Ti |
383 | 379 | // [END fs_paginate_cursor] |
384 | 380 | return Arrays.asList(firstPage, secondPage); |
385 | 381 | } |
| 382 | + |
| 383 | + void collectionGroupQuery() throws ExecutionException, InterruptedException { |
| 384 | + //CHECKSTYLE OFF: Indentation |
| 385 | + //CHECKSTYLE OFF: RightCurlyAlone |
| 386 | + // [START fs_collection_group_query_data_setup] |
| 387 | + CollectionReference cities = db.collection("cities"); |
| 388 | + |
| 389 | + final List<ApiFuture<WriteResult>> futures = Arrays.asList( |
| 390 | + cities.document("SF").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 391 | + put("name", "Golden Gate Bridge"); |
| 392 | + put("type", "bridge"); |
| 393 | + }}), |
| 394 | + cities.document("SF").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 395 | + put("name", "Legion of Honor"); |
| 396 | + put("type", "museum"); |
| 397 | + }}), |
| 398 | + cities.document("LA").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 399 | + put("name", "Griffith Park"); |
| 400 | + put("type", "park"); |
| 401 | + }}), |
| 402 | + cities.document("LA").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 403 | + put("name", "The Getty"); |
| 404 | + put("type", "museum"); |
| 405 | + }}), |
| 406 | + cities.document("DC").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 407 | + put("name", "Lincoln Memorial"); |
| 408 | + put("type", "memorial"); |
| 409 | + }}), |
| 410 | + cities.document("DC").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 411 | + put("name", "National Air and Space Museum"); |
| 412 | + put("type", "museum"); |
| 413 | + }}), |
| 414 | + cities.document("TOK").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 415 | + put("name", "Ueno Park"); |
| 416 | + put("type", "park"); |
| 417 | + }}), |
| 418 | + cities.document("TOK").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 419 | + put("name", "National Museum of Nature and Science"); |
| 420 | + put("type", "museum"); |
| 421 | + }}), |
| 422 | + cities.document("BJ").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 423 | + put("name", "Jingshan Park"); |
| 424 | + put("type", "park"); |
| 425 | + }}), |
| 426 | + cities.document("BJ").collection("landmarks").document().set(new HashMap<String, String>() {{ |
| 427 | + put("name", "Beijing Ancient Observatory"); |
| 428 | + put("type", "museum"); |
| 429 | + }}) |
| 430 | + ); |
| 431 | + final List<WriteResult> landmarks = ApiFutures.allAsList(futures).get(); |
| 432 | + // [END fs_collection_group_query_data_setup] |
| 433 | + |
| 434 | + // [START fs_collection_group_query] |
| 435 | + final Query museums = db.collectionGroup("landmarks").whereEqualTo("type", "museum"); |
| 436 | + final ApiFuture<QuerySnapshot> querySnapshot = museums.get(); |
| 437 | + for (DocumentSnapshot document : querySnapshot.get().getDocuments()) { |
| 438 | + System.out.println(document.getId()); |
| 439 | + } |
| 440 | + // [END fs_collection_group_query] |
| 441 | + //CHECKSTYLE ON: RightCurlyAlone |
| 442 | + //CHECKSTYLE ON: Indentation |
| 443 | + } |
386 | 444 | } |
0 commit comments