0

I am new to firestore and am wondering if anyone could tell me whether this solution is viable for a many-to-many relationship. I have a collection of Rosters and collection of Students which are related Many-to-Many. As the information I most frequently need about a student is just their name, would it be viable to have a map of students like {<StudentID> : "Student Name"} stored in rosters, and so if I want to retrieve more detailed information about students in a roster, I retrieve the map's keys and iterate through them to retrieve each student's document individually?

I am basing my solution off of this answer.

I'd greatly appreciate any advice! Thank you

Jonah Kornberg
  • 153
  • 1
  • 8

1 Answers1

0

Update to this, it is working fine. Here is my code for the cloud function to update athlete names if anyone in the future needs:

export const onUserUpdate =
functions.firestore.document("users/{user}/athletes/{athlete}").onUpdate(
    async (change) => {
      const after = change.after.data();
      const before = change.before.data();
      const bid = change.before.id;
      console.log("BID: ");
      console.log(bid);

      const userId: any = change.before.ref.parent.parent?.id;
      console.log(`users/${userId}/rosters`);
      if (after.athleteName != before.athleteName) {
        console.log("Change name detected");
        const snapshot =
      await db.collection(
          `users/${userId}/rosters`).where(
          `athletes.${bid}`, ">=", "").get();

        const updatePromises : Array<Promise<any>> = [];
        snapshot.forEach((doc) => {
          console.log(doc.id);
          updatePromises.push(db.collection(`users/${userId}/rosters`)
              .doc(doc.id).update(`athletes.${bid}`, after.athleteName)
          );
        });

        await Promise.all(updatePromises);
      }
    });
Jonah Kornberg
  • 153
  • 1
  • 8