【问题标题】:Designing many to many model with map使用地图设计多对多模型
【发布时间】:2021-04-27 21:53:59
【问题描述】:

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

我的解决方案基于this answer

如果有任何建议,我将不胜感激!谢谢

【问题讨论】:

    标签: google-cloud-firestore


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 1970-01-01
      • 2021-07-31
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多