1

Here is my Firestore database structure:

Faculty (Collection)
--Document1
  -memberId: "77777"
  -fullName: "Vince Melmar Ybanez"
--Document2
  -memberId: "12345"
  -fullName: "John Doe"
--Document3

AnnualDue (Sub-Collection of Document1 in Faculty)
--Document1
  -memberId: "77777"
  -fullName: "Vince Melmar Ybanez"
--Document2
  -memberId: "77777"
  -fullName: "Vince Melmar Ybanez"

AnnualDue (Sub-Collection of Document2 in Faculty)
--Document3
  -memberId: "12345"
  -fullName: "John Doe"

AnnualDue (Collection)
--Document1
  -memberId: "77777"
  -fullName: "Vince Melmar Ybanez"
--Document2
  -memberId: "77777"
  -fullName: "Vince Melmar Ybanez"
--Document3
  -memberId: "12345"
  -fullName: "John Doe"

How to update the fullName of member 1 (Document 1) from Vince Melmar Ybanez to Vince Ybanez in Faculty Collection, Annual Due Collection, and AnnualDue Sub-Collection?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Vince Ybañez
  • 103
  • 10
  • 1
    Have you consulted the [documentation on updating a document](https://firebase.google.com/docs/firestore/manage-data/add-data#update-data)? If so, what did you try, and what happened that didn't work the way you expect? Please edit the question with your code and debugging details. – Doug Stevenson Oct 16 '22 at 04:52

1 Answers1

1

If you're thinking of updating multiple documents that exist within multiple collections or subcollections in one go, please note that this is not possible. To solve this, you have to create three different operations.

Now to be able to update the "fullName" of "member 1" from "Vince Melmar Ybanez" to "Vince Ybanez" in the Faculty Collection, please use the following lines of code:

DocumentReference facultyDocOneRef = db.collection("Faculty").document("Document1");
Map<String, Object> update = new HashMap<>();
update.put("fullName", "Vince Ybanez");
facultyDocOneRef.update(update).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "Document updated successfully.");
        } else {
            Log.d(TAG, task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

Now to be able to update the remaining two fields within the other collections, you only need to create the references and use the exact same code as above:

DocumentReference annualDueDocOneRef = db.collection("AnnualDue").document("Document1");
annualDueDocOneRef.update(update).addOnCompleteListener(/*...*/);

And the last one:

DocumentReference facultyAnnualDueDocOneRef = db
           .collection("Faculty").document("Document1")
           .collection("AnnualDue").document("Document1");
facultyAnnualDueDocOneRef.update(update).addOnCompleteListener(/*...*/);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey Alex, Thanks for your answer! How to know if all the updates are successful so that I can display a success message to the user? – Vince Ybañez Oct 26 '22 at 15:41
  • I recently answered a similar [question](https://stackoverflow.com/questions/74163477/random-questions-from-different-child-from-firebase-to-java-andoird-studio/74209329#74209329). It's for reading data, but the same rules apply in case of updates. Give it a try and tell me if it works. – Alex Mamo Oct 26 '22 at 15:43
  • I get it. Thanks Alex! – Vince Ybañez Oct 26 '22 at 15:47
  • You're very welcome, Vince. – Alex Mamo Oct 26 '22 at 15:47