use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippetsIT method testWriteBatchIsSuccessful.
@Test
public void testWriteBatchIsSuccessful() throws Exception {
manageDataSnippets.writeBatch();
CollectionReference collection = db.collection("cities");
ApiFuture<DocumentSnapshot> document = collection.document("NYC").get();
assertTrue(document.get().exists());
DocumentReference documentReference = collection.document("SF");
Map<String, Object> data = getDocumentDataAsMap(documentReference);
assertTrue(data.containsKey("population"));
document = collection.document("LA").get();
assertFalse(document.get().exists());
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method updateSimpleDocument.
/**
* Partially update a document using the .update(field1, value1..) method.
*/
void updateSimpleDocument() throws Exception {
db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
// [START fs_update_doc]
// Update an existing document
DocumentReference docRef = db.collection("cities").document("DC");
// (async) Update one field
ApiFuture<WriteResult> future = docRef.update("capital", true);
// ...
WriteResult result = future.get();
System.out.println("Write result: " + result);
// [END fs_update_doc]
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method returnInfoFromTransaction.
/**
* Return information from a conditional transaction.
*
* @param population : set initial population.
*/
String returnInfoFromTransaction(long population) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("population", population);
// Block until transaction is complete is using transaction.get()
db.collection("cities").document("SF").set(map).get();
// [START fs_return_info_transaction]
final DocumentReference docRef = db.collection("cities").document("SF");
ApiFuture<String> transaction = db.runTransaction(new Transaction.Function<String>() {
@Override
public String updateCallback(Transaction transaction) throws Exception {
DocumentSnapshot snapshot = transaction.get(docRef).get();
Long newPopulation = snapshot.getLong("population") + 1;
// conditionally update based on current population
if (newPopulation <= 1000000L) {
transaction.update(docRef, "population", newPopulation);
return "Population increased to " + newPopulation;
} else {
throw new Exception("Sorry! Population is too big.");
}
}
});
// Print information retrieved from transaction
System.out.println(transaction.get());
// [END fs_return_info_transaction]
return transaction.get();
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method addDocumentDataAfterAutoGeneratingId.
/**
* Add data to a document after generating the document id.
*
* @return auto generated id
*/
String addDocumentDataAfterAutoGeneratingId() throws Exception {
City data = new City();
// [START fs_add_doc_data_after_auto_id]
// Add document data after generating an id.
DocumentReference addedDocRef = db.collection("cities").document();
System.out.println("Added document with ID: " + addedDocRef.getId());
// later...
ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
// [END fs_add_doc_data_after_auto_id]
// writeResult.get() blocks on operation
System.out.println("Update time : " + writeResult.get().getUpdateTime());
return addedDocRef.getId();
}
use of com.google.cloud.firestore.DocumentReference 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