use of com.google.cloud.firestore.CollectionReference in project java-firestore by googleapis.
the class QueryDataSnippets method createQueryAlternate.
/**
* Creates a sample query.
*
* @return query
*/
Query createQueryAlternate() throws Exception {
// [START fs_create_query_country]
// [START firestore_query_filter_eq_string]
// 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;
}
use of com.google.cloud.firestore.CollectionReference in project java-firestore by googleapis.
the class QueryDataSnippets method createOrderByCountryAndPopulation.
/**
* 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]
// [START firestore_query_order_multi]
Query query = cities.orderBy("state").orderBy("population", Direction.DESCENDING);
// [END fs_order_by_country_population]
return query;
}
use of com.google.cloud.firestore.CollectionReference in project java-firestore by googleapis.
the class QueryDataSnippets method createArrayQuery.
/**
* Creates a query based on array containment.
*
* @return query
*/
Query createArrayQuery() {
// [START fs_array_contains_filter]
// [START firestore_query_filter_array_contains]
CollectionReference citiesRef = db.collection("cities");
Query westCoastQuery = citiesRef.whereArrayContains("regions", "west_coast");
return westCoastQuery;
}
use of com.google.cloud.firestore.CollectionReference in project java-firestore by googleapis.
the class QueryDataSnippets method paginateCursor.
/**
* Example of a paginated query.
*/
List<Query> paginateCursor() throws InterruptedException, ExecutionException, TimeoutException {
// [START fs_paginate_cursor]
// [START firestore_query_cursor_pagination]
// Construct query for first 25 cities, ordered by population.
CollectionReference cities = db.collection("cities");
Query firstPage = cities.orderBy("population").limit(25);
// Wait for the results of the API call, waiting for a maximum of 30 seconds for a result.
ApiFuture<QuerySnapshot> future = firstPage.get();
List<QueryDocumentSnapshot> docs = future.get(30, TimeUnit.SECONDS).getDocuments();
// Construct query for the next 25 cities.
QueryDocumentSnapshot lastDoc = docs.get(docs.size() - 1);
Query secondPage = cities.orderBy("population").startAfter(lastDoc).limit(25);
future = secondPage.get();
docs = future.get(30, TimeUnit.SECONDS).getDocuments();
// [END fs_paginate_cursor]
return Arrays.asList(firstPage, secondPage);
}
use of com.google.cloud.firestore.CollectionReference in project java-firestore by googleapis.
the class QueryDataSnippets method collectionGroupQuery.
void collectionGroupQuery() throws ExecutionException, InterruptedException {
// CHECKSTYLE OFF: Indentation
// CHECKSTYLE OFF: RightCurlyAlone
// [START fs_collection_group_query_data_setup]
// [START firestore_query_collection_group_dataset]
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 firestore_query_collection_group_dataset]
// [END fs_collection_group_query_data_setup]
// [START fs_collection_group_query]
// [START firestore_query_collection_group_filter_eq]
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 firestore_query_collection_group_filter_eq]
// [END fs_collection_group_query]
// CHECKSTYLE ON: RightCurlyAlone
// CHECKSTYLE ON: Indentation
}
Aggregations