Search in sources :

Example 1 with QueryDocumentSnapshot

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

the class Quickstart method runAQuery.

void runAQuery() throws Exception {
    // [START fs_add_query]
    // asynchronously query for all users born before 1900
    ApiFuture<QuerySnapshot> query = db.collection("users").whereLessThan("born", 1900).get();
    // ...
    // query.get() blocks on response
    QuerySnapshot querySnapshot = query.get();
    List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
    for (QueryDocumentSnapshot document : documents) {
        System.out.println("User: " + document.getId());
        System.out.println("First: " + document.getString("first"));
        if (document.contains("middle")) {
            System.out.println("Middle: " + document.getString("middle"));
        }
        System.out.println("Last: " + document.getString("last"));
        System.out.println("Born: " + document.getLong("born"));
    }
// [END fs_add_query]
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 2 with QueryDocumentSnapshot

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

the class RetrieveDataSnippets method getAllDocuments.

/**
 * Return all documents in the cities collection.
 *
 * @return list of documents
 */
public List<QueryDocumentSnapshot> getAllDocuments() throws Exception {
    // [START fs_get_all_docs]
    // asynchronously retrieve all documents
    ApiFuture<QuerySnapshot> future = db.collection("cities").get();
    // future.get() blocks on response
    List<QueryDocumentSnapshot> documents = future.get().getDocuments();
    for (QueryDocumentSnapshot document : documents) {
        System.out.println(document.getId() + " => " + document.toObject(City.class));
    }
    // [END fs_get_all_docs]
    return documents;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 3 with QueryDocumentSnapshot

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

the class ManageDataSnippets method deleteCollection.

// [START fs_delete_collection]
/**
 * Delete a collection in batches to avoid out-of-memory errors.
 * Batch size may be tuned based on document size (atmost 1MB) and application requirements.
 */
void deleteCollection(CollectionReference collection, int batchSize) {
    try {
        // retrieve a small batch of documents to avoid out-of-memory errors
        ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
        int deleted = 0;
        // future.get() blocks on document retrieval
        List<QueryDocumentSnapshot> documents = future.get().getDocuments();
        for (QueryDocumentSnapshot document : documents) {
            document.getReference().delete();
            ++deleted;
        }
        if (deleted >= batchSize) {
            // retrieve and delete another batch
            deleteCollection(collection, batchSize);
        }
    } catch (Exception e) {
        System.err.println("Error deleting collection : " + e.getMessage());
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 4 with QueryDocumentSnapshot

use of com.google.cloud.firestore.QueryDocumentSnapshot 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 5 with QueryDocumentSnapshot

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

the class RetrieveDataSnippetsIT method testRetrieveQueryResults.

@Test
public void testRetrieveQueryResults() throws Exception {
    List<QueryDocumentSnapshot> docs = retrieveDataSnippets.getQueryResults();
    assertEquals(docs.size(), 3);
    Set<String> docIds = new HashSet<>();
    for (DocumentSnapshot doc : docs) {
        docIds.add(doc.getId());
    }
    assertTrue(docIds.contains("BJ") && docIds.contains("TOK") && docIds.contains("DC"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) HashSet(java.util.HashSet) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Aggregations

QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)11 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)8 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)4 Test (org.junit.Test)4 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)3 ExecutionException (java.util.concurrent.ExecutionException)3 Firestore (com.google.cloud.firestore.Firestore)2 HashSet (java.util.HashSet)2 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 ApiFuture (com.google.api.core.ApiFuture)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Query (com.google.cloud.firestore.Query)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BatchGetDocumentsRequest (com.google.firestore.v1.BatchGetDocumentsRequest)1 Document (com.google.firestore.v1.Document)1 ListCollectionIdsRequest (com.google.firestore.v1.ListCollectionIdsRequest)1 ListDocumentsRequest (com.google.firestore.v1.ListDocumentsRequest)1 PartitionQueryRequest (com.google.firestore.v1.PartitionQueryRequest)1 Precondition (com.google.firestore.v1.Precondition)1 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)1