Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firestore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>0.81.0-beta</version>
<version>1.2.0</version>
</dependency>
<!-- [END fs-maven] -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,19 @@
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.DocumentReference;
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.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.WriteResult;

import com.google.firestore.v1beta1.Document;
import com.google.protobuf.Api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -383,4 +379,66 @@ List<Query> paginateCursor() throws InterruptedException, ExecutionException, Ti
// [END fs_paginate_cursor]
return Arrays.asList(firstPage, secondPage);
}

void collectionGroupQuery() throws ExecutionException, InterruptedException {
//CHECKSTYLE OFF: Indentation
//CHECKSTYLE OFF: RightCurlyAlone
// [START fs_collection_group_query_data_setup]
CollectionReference cities = db.collection("cities");

final List<ApiFuture<WriteResult>> futures = Arrays.asList(
cities.document("SF").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Golden Gate Bridge");
put("type", "bridge");
}}),
cities.document("SF").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Legion of Honor");
put("type", "museum");
}}),
cities.document("LA").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Griffith Park");
put("type", "park");
}}),
cities.document("LA").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "The Getty");
put("type", "museum");
}}),
cities.document("DC").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Lincoln Memorial");
put("type", "memorial");
}}),
cities.document("DC").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "National Air and Space Museum");
put("type", "museum");
}}),
cities.document("TOK").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Ueno Park");
put("type", "park");
}}),
cities.document("TOK").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "National Museum of Nature and Science");
put("type", "museum");
}}),
cities.document("BJ").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Jingshan Park");
put("type", "park");
}}),
cities.document("BJ").collection("landmarks").document().set(new HashMap<String, String>() {{
put("name", "Beijing Ancient Observatory");
put("type", "museum");
}})
);
final List<WriteResult> landmarks = ApiFutures.allAsList(futures).get();
// [END fs_collection_group_query_data_setup]

// [START fs_collection_group_query]
final Query museums = db.collectionGroup("landmarks").whereEqualTo("type", "museum");
final ApiFuture<QuerySnapshot> querySnapshot = museums.get();
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
System.out.println(document.getId());
}
// [END fs_collection_group_query]
//CHECKSTYLE ON: RightCurlyAlone
//CHECKSTYLE ON: Indentation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,9 @@ public boolean equals(Object obj) {
&& Objects.equals(capital, city.capital)
&& Objects.equals(regions, city.regions);
}

@Override
public int hashCode() {
return Objects.hash(name, state, country, capital, population, regions);
}
}