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

The following examples show how to use com.google.firebase.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: DocSnippets.java    From snippets-android with Apache License 2.0 5 votes vote down vote up
public void updateDocumentArray() {
    // [START update_document_array]
    DocumentReference washingtonRef = db.collection("cities").document("DC");

    // Atomically add a new region to the "regions" array field.
    washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));

    // Atomically remove a region from the "regions" array field.
    washingtonRef.update("regions", FieldValue.arrayRemove("east_coast"));
    // [END update_document_array]
}
 
Example 2
Source File: DocSnippets.java    From snippets-android with Apache License 2.0 5 votes vote down vote up
public void updateDocumentIncrement() {
    // [START update_document_increment]
    DocumentReference washingtonRef = db.collection("cities").document("DC");

    // Atomically increment the population of the city by 50.
    washingtonRef.update("population", FieldValue.increment(50));
    // [END update_document_increment]
}
 
Example 3
Source File: FirestoreTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void updateShouldTriggerListenerWithUpdatedData() throws Exception {
  FirebaseAuth auth = FirebaseAuth.getInstance();
  FirebaseFirestore firestore = FirebaseFirestore.getInstance();

  auth.signOut();
  Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password");
  Tasks2.waitForSuccess(signInTask);

  DocumentReference doc = firestore.collection("restaurants").document(TestId.create());
  try {
    HashMap<String, Object> originalData = new HashMap<>();
    originalData.put("location", "Google NYC");

    Task<?> setTask = doc.set(new HashMap<>(originalData));
    Tasks2.waitForSuccess(setTask);

    SnapshotListener listener = new SnapshotListener(2);
    ListenerRegistration registration = doc.addSnapshotListener(listener);

    try {
      HashMap<String, Object> updateData = new HashMap<>();
      updateData.put("priority", 5L);

      Task<?> updateTask = doc.update(new HashMap<>(updateData));
      Task<DocumentSnapshot> snapshotTask = listener.toTask();
      Tasks2.waitForSuccess(updateTask);
      Tasks2.waitForSuccess(snapshotTask);

      DocumentSnapshot result = snapshotTask.getResult();
      HashMap<String, Object> finalData = new HashMap<>();
      finalData.put("location", "Google NYC");
      finalData.put("priority", 5L);
      assertThat(result.getData()).isEqualTo(finalData);
    } finally {
      registration.remove();
    }
  } finally {
    Tasks2.waitBestEffort(doc.delete());
  }
}
 
Example 4
Source File: SolutionCounters.java    From snippets-android with Apache License 2.0 4 votes vote down vote up
public Task<Void> incrementCounter(final DocumentReference ref, final int numShards) {
    int shardId = (int) Math.floor(Math.random() * numShards);
    DocumentReference shardRef = ref.collection("shards").document(String.valueOf(shardId));
    
    return shardRef.update("count", FieldValue.increment(1));
}