use of com.google.cloud.firestore.WriteResult in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method deleteFields.
/**
* Delete specific fields when updating a document.
*/
void deleteFields() throws Exception {
City city = new City("Beijing");
city.setCapital(true);
db.collection("cities").document("BJ").set(city).get();
// [START fs_delete_fields]
DocumentReference docRef = db.collection("cities").document("BJ");
Map<String, Object> updates = new HashMap<>();
updates.put("capital", FieldValue.delete());
// Update and delete the "capital" field in the document
ApiFuture<WriteResult> writeResult = docRef.update(updates);
System.out.println("Update time : " + writeResult.get());
// [END fs_delete_fields]
}
use of com.google.cloud.firestore.WriteResult in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method updateUsingMap.
/**
* Partially update fields of a document using a map (field => value).
*/
void updateUsingMap() throws Exception {
db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
// [START fs_update_doc_map]
// update multiple fields using a map
DocumentReference docRef = db.collection("cities").document("DC");
Map<String, Object> updates = new HashMap<>();
updates.put("name", "Washington D.C.");
updates.put("country", "USA");
updates.put("capital", true);
// asynchronously update doc
ApiFuture<WriteResult> writeResult = docRef.update(updates);
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END fs_update_doc_map]
}
use of com.google.cloud.firestore.WriteResult in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method updateNestedFields.
/**
* Partial update nested fields of a document.
*/
void updateNestedFields() throws Exception {
// CHECKSTYLE OFF: VariableDeclarationUsageDistance
// [START fs_update_nested_fields]
// Create an initial document to update
DocumentReference frankDocRef = db.collection("users").document("frank");
Map<String, Object> initialData = new HashMap<>();
initialData.put("name", "Frank");
initialData.put("age", 12);
Map<String, Object> favorites = new HashMap<>();
favorites.put("food", "Pizza");
favorites.put("color", "Blue");
favorites.put("subject", "Recess");
initialData.put("favorites", favorites);
ApiFuture<WriteResult> initialResult = frankDocRef.set(initialData);
// Confirm that data has been successfully saved by blocking on the operation
initialResult.get();
// Update age and favorite color
Map<String, Object> updates = new HashMap<>();
updates.put("age", 13);
updates.put("favorites.color", "Red");
// Async update document
ApiFuture<WriteResult> writeResult = frankDocRef.update(updates);
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END fs_update_nested_fields]
// CHECKSTYLE ON: VariableDeclarationUsageDistance
}
use of com.google.cloud.firestore.WriteResult in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method addSimpleDocumentAsEntity.
/**
* Add a document to a collection as a custom object.
*
* @return entity added
*/
City addSimpleDocumentAsEntity() throws Exception {
// [START fs_add_simple_doc_as_entity]
City city = new City("Los Angeles", "CA", "USA", false, 3900000L);
ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(city);
// block on response if required
System.out.println("Update time : " + future.get().getUpdateTime());
return city;
}
use of com.google.cloud.firestore.WriteResult 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]
}
Aggregations