Search in sources :

Example 1 with WriteResult

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]
}
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 2 with WriteResult

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]
}
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 3 with WriteResult

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

Example 4 with WriteResult

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

Example 5 with WriteResult

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

Aggregations

WriteResult (com.google.cloud.firestore.WriteResult)12 DocumentReference (com.google.cloud.firestore.DocumentReference)8 City (com.example.firestore.snippets.model.City)6 HashMap (java.util.HashMap)5 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Firestore (com.google.cloud.firestore.Firestore)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 Translate (com.google.cloud.translate.Translate)1 Translation (com.google.cloud.translate.Translation)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1