Search in sources :

Example 26 with Query

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

the class QueryDataSnippets method createRangeQuery.

/**
 * An instance of a valid range/inequality query : range operators are limited to a single field.
 *
 * @return query
 */
Query createRangeQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_range_query]
    Query validQuery1 = cities.whereGreaterThanOrEqualTo("state", "CA").whereLessThanOrEqualTo("state", "IN");
    Query validQuery2 = cities.whereEqualTo("state", "CA").whereGreaterThan("population", 1000000);
    // [END fs_range_query]
    return validQuery1;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 27 with Query

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

the class QueryDataSnippets method createInvalidRangeQuery.

/**
 * An instance of an invalid range query : range operators are limited to a single field.
 *
 * @return query
 */
Query createInvalidRangeQuery() {
    CollectionReference cities = db.collection("cities");
    // Violates constraint : range operators are limited to a single field
    // [START fs_invalid_range_query]
    Query invalidRangeQuery = cities.whereGreaterThanOrEqualTo("state", "CA").whereGreaterThan("population", 100000);
    // [END fs_invalid_range_query]
    return invalidRangeQuery;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 28 with Query

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

the class QueryDataSnippets method createAQueryAlternate.

/**
 * Creates a sample query.
 *
 * @return query
 */
Query createAQueryAlternate() throws Exception {
    // [START fs_create_query_country]
    // 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 : 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 29 with Query

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

the class QueryDataSnippets method createOrderByNameWithLimitQuery.

/**
 * Creates a query that combines order by with limit.
 *
 * @return query
 */
Query createOrderByNameWithLimitQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_order_by_name_limit_query]
    Query query = cities.orderBy("name").limit(3);
    // [END fs_order_by_name_limit_query]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 30 with Query

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

the class QueryDataSnippets method createOrderByNameDescWithLimitQuery.

/**
 * Creates a query that combines order by in descending order with the limit operator.
 *
 * @return query
 */
Query createOrderByNameDescWithLimitQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_order_by_name_desc_limit_query]
    Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
    // [END fs_order_by_name_desc_limit_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