Search in sources :

Example 11 with Query

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

the class QueryDataSnippets method createStartAtFieldQueryCursor.

/**
 * Create a query defining the start point of a query.
 *
 * @return query
 */
Query createStartAtFieldQueryCursor() {
    CollectionReference cities = db.collection("cities");
    // [START fs_start_at_field_query_cursor]
    Query query = cities.orderBy("population").startAt(4921000L);
    // [END fs_start_at_field_query_cursor]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 12 with Query

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

the class QueryDataSnippets method createWhereWithOrderByAndLimitQuery.

/**
 * Creates a query that combines where clause with order by and limit operator.
 *
 * @return query
 */
Query createWhereWithOrderByAndLimitQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_where_order_by_limit_query]
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
    // [END fs_where_order_by_limit_query]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 13 with Query

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

the class QueryDataSnippetsIT method testMultipleCursorConditions.

@Test
public void testMultipleCursorConditions() throws Exception {
    // populate us_cities collection
    Map<String, String> city1 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Massachusetts").build();
    Map<String, String> city2 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Missouri").build();
    Map<String, String> city3 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Wisconsin").build();
    db.collection("us_cities").document("Massachusetts").set(city1).get();
    db.collection("us_cities").document("Missouri").set(city2).get();
    db.collection("us_cities").document("Wisconsin").set(city3).get();
    Query query1 = db.collection("us_cities").orderBy("name").orderBy("state").startAt("Springfield");
    // all documents are retrieved
    QuerySnapshot querySnapshot = query1.get().get();
    List<QueryDocumentSnapshot> docs = querySnapshot.getDocuments();
    assertEquals(3, docs.size());
    // Will return "Springfield, Missouri" and "Springfield, Wisconsin"
    Query query2 = db.collection("us_cities").orderBy("name").orderBy("state").startAt("Springfield", "Missouri");
    // only Missouri and Wisconsin are retrieved
    List<String> expectedResults = Arrays.asList("Missouri", "Wisconsin");
    List<String> result = getResults(query2);
    assertTrue(Objects.equals(result, expectedResults));
}
Also used : Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) ImmutableMap(com.google.common.collect.ImmutableMap) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 14 with Query

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

the class QueryDataSnippetsIT method testRangeQuery.

@Test
public void testRangeQuery() throws Exception {
    Query q = queryDataSnippets.createRangeQuery();
    Set<String> result = getResultsAsSet(q);
    Set<String> expectedResults = new HashSet<>(Arrays.asList("SF", "LA"));
    assertTrue(Objects.equals(result, expectedResults));
}
Also used : Query(com.google.cloud.firestore.Query) HashSet(java.util.HashSet) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 15 with Query

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

the class QueryDataSnippetsIT method testRangeWithOrderByQuery.

@Test
public void testRangeWithOrderByQuery() throws Exception {
    Query q = queryDataSnippets.createRangeWithOrderByQuery();
    List<String> result = getResults(q);
    List<String> expectedResults = Arrays.asList("LA", "TOK", "BJ");
    assertEquals(result, expectedResults);
}
Also used : Query(com.google.cloud.firestore.Query) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

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