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]
}
Aggregations