Java Code Examples for com.google.cloud.firestore.DocumentReference#update()

The following examples show how to use com.google.cloud.firestore.DocumentReference#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** 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]
}
 
Example 2
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Update array fields in a document. **/
void updateDocumentArray() throws Exception {
  // [START fs_update_document_array]
  DocumentReference washingtonRef = db.collection("cities").document("DC");

  // Atomically add a new region to the "regions" array field.
  ApiFuture<WriteResult> arrayUnion = washingtonRef.update("regions",
      FieldValue.arrayUnion("greater_virginia"));
  System.out.println("Update time : " + arrayUnion.get());

  // Atomically remove a region from the "regions" array field.
  ApiFuture<WriteResult> arrayRm = washingtonRef.update("regions",
      FieldValue.arrayRemove("east_coast"));
  System.out.println("Update time : " + arrayRm.get());
  // [END fs_update_document_array]
}
 
Example 3
Source File: FirestoreDataSink.java    From daq with Apache License 2.0 5 votes vote down vote up
public void validationResult(String deviceId, String schemaId, Map<String, String> attributes,
    Object message,
    ErrorTree errorTree) {
  if (oldError.get() != null) {
    throw oldError.getAndSet(null);
  }

  try {
    String registryId = attributes.get("deviceRegistryId");
    Preconditions.checkNotNull(deviceId, "deviceId attribute not defined");
    Preconditions.checkNotNull(schemaId, "schemaId not properly defined");
    Preconditions.checkNotNull(registryId, "deviceRegistryId attribute not defined");
    String instantNow = dateTimeFormatter.format(Instant.now());
    DocumentReference registryDoc = db.collection("registries").document(registryId);
    registryDoc.update("validated", instantNow);
    DocumentReference deviceDoc = registryDoc.collection("devices").document(deviceId);
    deviceDoc.update("validated", instantNow);
    DocumentReference resultDoc = deviceDoc.collection("validations").document(schemaId);
    PojoBundle dataBundle = new PojoBundle();
    dataBundle.validated = instantNow;
    dataBundle.errorTree = errorTree;
    dataBundle.attributes = attributes;
    dataBundle.message = message;
    resultDoc.set(dataBundle);
  } catch (Exception e) {
    throw new RuntimeException("While writing result for " + deviceId, e);
  }
}
 
Example 4
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** 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]
}
 
Example 5
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** 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
}
 
Example 6
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** Update document with server timestamp. */
void updateServerTimestamp() throws Exception {
  db.collection("objects").document("some-id").set(new HashMap<String, Object>()).get();

  // [START fs_update_server_timestamp]
  DocumentReference docRef = db.collection("objects").document("some-id");
  // Update the timestamp field with the value from the server
  ApiFuture<WriteResult> writeResult = docRef.update("timestamp", FieldValue.serverTimestamp());
  System.out.println("Update time : " + writeResult.get());
  // [END fs_update_server_timestamp]
}
 
Example 7
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** 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]
}
 
Example 8
Source File: ManageDataSnippets.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public void updateDocumentIncrement() throws ExecutionException, InterruptedException {
  final City city = new City();
  city.setPopulation(100L);
  db.collection("cities").document("DC").set(city).get();

  // [START fs_update_document_increment]
  DocumentReference washingtonRef = db.collection("cities").document("DC");

  // Atomically increment the population of the city by 50.
  final ApiFuture<WriteResult> updateFuture = washingtonRef
      .update("population", FieldValue.increment(50));
  // [END fs_update_document_increment]
  updateFuture.get();
}
 
Example 9
Source File: MyDataStore.java    From smart-home-java with Apache License 2.0 4 votes vote down vote up
public void setHomegraph(String userId, Boolean enable) {
  DocumentReference user = database.collection("users").document(userId);
  user.update("homegraph", enable);
}