Search in sources :

Example 1 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class ListenDataSnippets method detachListener.

/**
 * Demonstrate how to detach an event listener.
 */
void detachListener() {
    // [START detach_errors]
    Query query = db.collection("cities");
    ListenerRegistration registration = query.addSnapshotListener(new EventListener<QuerySnapshot>() {

        // [START_EXCLUDE]
        @Override
        public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirestoreException e) {
        }
    });
    // ...
    // Stop listening to changes
    registration.remove();
// [END detach_errors]
}
Also used : Query(com.google.cloud.firestore.Query) FirestoreException(com.google.cloud.firestore.FirestoreException) ListenerRegistration(com.google.cloud.firestore.ListenerRegistration) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 2 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippets method createAQuery.

/**
 * Creates a sample query.
 *
 * @return query
 */
Query createAQuery() throws Exception {
    // [START fs_create_query]
    // Create a reference to the cities collection
    CollectionReference cities = db.collection("cities");
    // Create a query against the collection.
    Query query = cities.whereEqualTo("capital", true);
    // 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]
    return query;
}
Also used : 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 3 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippets method createSimpleQueries.

/**
 * Creates queries with simple where clauses.
 *
 * @return queries
 */
List<Query> createSimpleQueries() {
    List<Query> querys = new ArrayList<>();
    CollectionReference cities = db.collection("cities");
    // [START fs_simple_queries]
    Query countryQuery = cities.whereEqualTo("state", "CA");
    Query populationQuery = cities.whereLessThan("population", 1000000L);
    Query cityQuery = cities.whereGreaterThanOrEqualTo("name", "San Francisco");
    // [END fs_simple_queries]
    querys.add(countryQuery);
    querys.add(populationQuery);
    querys.add(cityQuery);
    return querys;
}
Also used : Query(com.google.cloud.firestore.Query) ArrayList(java.util.ArrayList) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 4 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

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]
    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 5 with Query

use of com.google.cloud.firestore.Query in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippets method createRangeWithOrderByQuery.

/**
 * Creates a query using a range where clause with order by. Order by must be based on the same
 * field as the range clause.
 *
 * @return query
 */
Query createRangeWithOrderByQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_range_order_by_query]
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population");
    // [END fs_range_order_by_query]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Aggregations

Query (com.google.cloud.firestore.Query)31 CollectionReference (com.google.cloud.firestore.CollectionReference)15 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)12 Test (org.junit.Test)12 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)6 HashSet (java.util.HashSet)3 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)2 ExecutionException (java.util.concurrent.ExecutionException)2 FirestoreException (com.google.cloud.firestore.FirestoreException)1 ListenerRegistration (com.google.cloud.firestore.ListenerRegistration)1 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1