0

How can I change multiple documents? I have 2 collections firestore. One (users) is for user account (email, name, etc).

To second collections (dashboard) users can add some message to a board. In this second collection is name, category, time etc.

What I want to do is when someone change his name or faculty in account, it will also change in second collection for each his comment so it will show up to date information

DocumentReference documentReference = fStore.collection("users").document(user.getUid());
Map<String, Object> edited = new HashMap<>();
edited.put("email",email);
edited.put("smallName",StringUtils.unaccent(profileName.getText().toString()).toLowerCase());
edited.put("fullName",profileName.getText().toString());
edited.put("fakulta",mySpinner.getSelectedItem().toString());
documentReference.update(edited).addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        Toast.makeText(EditProfile.this, "Profile data are changed", Toast.LENGTH_SHORT).show();
        //startActivity(new Intent(getApplicationContext(),MainActivity.class));
        finish();
    }

Firestore

Is there any way to do it without changing the whole structure of my app?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • hey, unfortunatly I run out of time before i noticed your answer, so i turn it on. Its minor bug so i hope it will not have great impact for the evaluation of the bachelor thesis. After i finish my exams i will try fix it and i will let you know. Thnx anyway – Matúš Domorák Jun 06 '21 at 10:23

1 Answers1

0

To update the user names in the comment docs, you'll need to:

  1. Query the collection to find the comments by the user who updated their name. If you stored the UID for each user in the comment document, this would look something like fStore.collection("dashboard").where("uid", "==", "theUidOfTheUserWhoUpdatedTheirName"). If you didn't store their UID, you'll have to query on the old value of their name instead, but the code will be similar.
  2. Loop over the query results and update each document in turn. If you have a lot of these, you may want to read about the fastest way to do this here: What is the fastest way to write a lot of documents to Firestore?
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807