Skip to content

Commit 1f0a55e

Browse files
BenWhiteheadkurtisvg
authored andcommitted
Add snippet for Collection Group Query (GoogleCloudPlatform#1399)
* Bump `com.google.cloud:google-cloud-firestore` to `1.2.0` which includes the new functionality * Add `hashCode` for `City.java`
1 parent 9a11673 commit 1f0a55e

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

firestore/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.google.cloud</groupId>
4747
<artifactId>google-cloud-firestore</artifactId>
48-
<version>0.81.0-beta</version>
48+
<version>1.2.0</version>
4949
</dependency>
5050
<!-- [END fs-maven] -->
5151

firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,19 @@
1717
package com.example.firestore.snippets;
1818

1919
import com.example.firestore.snippets.model.City;
20-
2120
import com.google.api.core.ApiFuture;
2221
import com.google.api.core.ApiFutures;
2322
import com.google.cloud.firestore.CollectionReference;
24-
import com.google.cloud.firestore.DocumentReference;
2523
import com.google.cloud.firestore.DocumentSnapshot;
2624
import com.google.cloud.firestore.Firestore;
2725
import com.google.cloud.firestore.Query;
2826
import com.google.cloud.firestore.Query.Direction;
2927
import com.google.cloud.firestore.QueryDocumentSnapshot;
3028
import com.google.cloud.firestore.QuerySnapshot;
3129
import com.google.cloud.firestore.WriteResult;
32-
33-
import com.google.firestore.v1beta1.Document;
34-
import com.google.protobuf.Api;
3530
import java.util.ArrayList;
3631
import java.util.Arrays;
32+
import java.util.HashMap;
3733
import java.util.List;
3834
import java.util.concurrent.ExecutionException;
3935
import java.util.concurrent.TimeUnit;
@@ -383,4 +379,66 @@ List<Query> paginateCursor() throws InterruptedException, ExecutionException, Ti
383379
// [END fs_paginate_cursor]
384380
return Arrays.asList(firstPage, secondPage);
385381
}
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+
}
386444
}

firestore/src/main/java/com/example/firestore/snippets/model/City.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,9 @@ public boolean equals(Object obj) {
161161
&& Objects.equals(capital, city.capital)
162162
&& Objects.equals(regions, city.regions);
163163
}
164+
165+
@Override
166+
public int hashCode() {
167+
return Objects.hash(name, state, country, capital, population, regions);
168+
}
164169
}

0 commit comments

Comments
 (0)