Search in sources :

Example 6 with DocumentReference

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]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) HashMap(java.util.HashMap) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 7 with DocumentReference

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
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) HashMap(java.util.HashMap) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 8 with DocumentReference

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)));
}
Also used : DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 9 with DocumentReference

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);
}
Also used : DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 10 with DocumentReference

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)));
}
Also used : DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Aggregations

DocumentReference (com.google.cloud.firestore.DocumentReference)26 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)14 Test (org.junit.Test)14 City (com.example.firestore.snippets.model.City)12 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)7 WriteResult (com.google.cloud.firestore.WriteResult)7 HashMap (java.util.HashMap)5 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)4 Transaction (com.google.cloud.firestore.Transaction)2 CollectionReference (com.google.cloud.firestore.CollectionReference)1 FirestoreException (com.google.cloud.firestore.FirestoreException)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1