首页 > 解决方案 > 使用地图设计多对多模型

问题描述

我是firestore的新手,想知道是否有人可以告诉我这个解决方案是否适用于多对多关系。我有一组花名册和一组与多对多相关的学生。由于我最常需要的关于学生的信息只是他们的姓名,将像 {<StudentID> : "Student Name"} 这样的学生地图存储在名册中是否可行,因此如果我想检索更详细的信息关于名册中的学生,我检索地图的键并遍历它们以单独检索每个学生的文档?

我的解决方案基于这个答案

我将不胜感激任何建议!谢谢

标签: google-cloud-firestore

解决方案


对此进行更新,它工作正常。如果将来有人需要,这是我的云功能代码,用于更新运动员姓名:

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);
      }
    });

推荐阅读