Search in sources :

Example 16 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method writeBatch.

/**
 * Write documents in a batch.
 */
void writeBatch() throws Exception {
    db.collection("cities").document("SF").set(new City()).get();
    db.collection("cities").document("LA").set(new City()).get();
    // [START fs_write_batch]
    // Get a new write batch
    WriteBatch batch = db.batch();
    // Set the value of 'NYC'
    DocumentReference nycRef = db.collection("cities").document("NYC");
    batch.set(nycRef, new City());
    // Update the population of 'SF'
    DocumentReference sfRef = db.collection("cities").document("SF");
    batch.update(sfRef, "population", 1000000L);
    // Delete the city 'LA'
    DocumentReference laRef = db.collection("cities").document("LA");
    batch.delete(laRef);
    // asynchronously commit the batch
    ApiFuture<List<WriteResult>> future = batch.commit();
    // future.get() blocks on batch commit operation
    for (WriteResult result : future.get()) {
        System.out.println("Update time : " + result.getUpdateTime());
    }
// [END fs_write_batch]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) ArrayList(java.util.ArrayList) List(java.util.List) City(com.example.firestore.snippets.model.City) WriteBatch(com.google.cloud.firestore.WriteBatch) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

City (com.example.firestore.snippets.model.City)16 DocumentReference (com.google.cloud.firestore.DocumentReference)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)6 WriteResult (com.google.cloud.firestore.WriteResult)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 ApiFuture (com.google.api.core.ApiFuture)2 CollectionReference (com.google.cloud.firestore.CollectionReference)2 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)2 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)2 HashMap (java.util.HashMap)2 Transaction (com.google.cloud.firestore.Transaction)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 List (java.util.List)1