Search in sources :

Example 11 with QuerySnapshot

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

the class QuickstartIT method deleteAllDocuments.

private void deleteAllDocuments() throws Exception {
    ApiFuture<QuerySnapshot> future = db.collection("users").get();
    QuerySnapshot querySnapshot = future.get();
    for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
        // block on delete operation
        db.document("users/" + doc.getId()).delete().get();
    }
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 12 with QuerySnapshot

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

the class Quickstart method retrieveAllDocuments.

void retrieveAllDocuments() throws Exception {
    // [START fs_get_all]
    // asynchronously retrieve all users
    ApiFuture<QuerySnapshot> query = db.collection("users").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_get_all]
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 13 with QuerySnapshot

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

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

the class RetrieveDataSnippets method getQueryResults.

/**
 * Return multiple documents from a collection based on a query.
 *
 * @return list of documents of capital cities.
 */
public List<QueryDocumentSnapshot> getQueryResults() throws Exception {
    // [START fs_get_multiple_docs]
    // asynchronously retrieve multiple documents
    ApiFuture<QuerySnapshot> future = db.collection("cities").whereEqualTo("capital", true).get();
    // future.get() blocks on response
    List<QueryDocumentSnapshot> documents = future.get().getDocuments();
    for (DocumentSnapshot document : documents) {
        System.out.println(document.getId() + " => " + document.toObject(City.class));
    }
    // [END fs_get_multiple_docs]
    return documents;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 15 with QuerySnapshot

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

the class BaseIntegrationTest method deleteAllDocuments.

protected static void deleteAllDocuments(Firestore db) throws Exception {
    ApiFuture<QuerySnapshot> future = db.collection("cities").get();
    QuerySnapshot querySnapshot = future.get();
    for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
        // block on delete operation
        db.collection("cities").document(doc.getId()).delete().get();
    }
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Aggregations

QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)17 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)10 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)8 Query (com.google.cloud.firestore.Query)6 ExecutionException (java.util.concurrent.ExecutionException)4 CollectionReference (com.google.cloud.firestore.CollectionReference)3 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 ApiFuture (com.google.api.core.ApiFuture)1 Firestore (com.google.cloud.firestore.Firestore)1 FirestoreException (com.google.cloud.firestore.FirestoreException)1 ListenerRegistration (com.google.cloud.firestore.ListenerRegistration)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