use of com.google.cloud.firestore.DocumentReference 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.DocumentReference 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.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ListenDataSnippetsIT method testListenDocument.
@Test
public void testListenDocument() throws Exception {
Map<String, Object> expectedData = listenDataSnippets.listenToDocument();
DocumentReference docRef = db.collection("cities").document("SF");
assertTrue(Objects.equals(expectedData, getDocumentDataAsMap(docRef)));
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippetsIT method testSimpleTransaction.
@Test
public void testSimpleTransaction() throws Exception {
DocumentReference docRef = db.collection("cities").document("SF");
ApiFuture<Void> future = manageDataSnippets.runSimpleTransaction();
future.get();
Map<String, Object> data = getDocumentDataAsMap(docRef);
assertEquals(data.get("population"), 860000L + 1L);
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippetsIT method testAddDatasMap.
@Test
public void testAddDatasMap() throws Exception {
Map<String, Object> expectedData = manageDataSnippets.addSimpleDocumentAsMap();
DocumentReference docRef = db.collection("cities").document("LA");
assertTrue(Objects.equals(expectedData, getDocumentDataAsMap(docRef)));
}
Aggregations