Search in sources :

Example 61 with CollectionReference

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;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 62 with CollectionReference

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;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 63 with CollectionReference

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;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 64 with CollectionReference

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);
}
Also used : Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 65 with CollectionReference

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
}
Also used : ApiFuture(com.google.api.core.ApiFuture) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) Query(com.google.cloud.firestore.Query) HashMap(java.util.HashMap) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Aggregations

CollectionReference (com.google.cloud.firestore.CollectionReference)72 Query (com.google.cloud.firestore.Query)48 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)16 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)12 DocumentReference (com.google.cloud.firestore.DocumentReference)11 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)11 ArrayList (java.util.ArrayList)11 ExecutionException (java.util.concurrent.ExecutionException)9 Test (org.junit.Test)9 WriteResult (com.google.cloud.firestore.WriteResult)7 DatasetDataProject (bio.terra.service.dataset.DatasetDataProject)6 ApiFuture (com.google.api.core.ApiFuture)6 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)5 HashMap (java.util.HashMap)5 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)4 City (com.example.firestore.snippets.model.City)4 Firestore (com.google.cloud.firestore.Firestore)4 FileSystemCorruptException (bio.terra.service.filedata.exception.FileSystemCorruptException)2 TranslateMessage (com.getstarted.background.objects.TranslateMessage)2 FirestoreOptions (com.google.cloud.firestore.FirestoreOptions)2