google-cloud-firestore - 使用地图设计多对多模型
问题描述
我是firestore的新手,想知道是否有人可以告诉我这个解决方案是否适用于多对多关系。我有一组花名册和一组与多对多相关的学生。由于我最常需要的关于学生的信息只是他们的姓名,将像 {<StudentID> : "Student Name"} 这样的学生地图存储在名册中是否可行,因此如果我想检索更详细的信息关于名册中的学生,我检索地图的键并遍历它们以单独检索每个学生的文档?
我的解决方案基于这个答案。
我将不胜感激任何建议!谢谢
解决方案
对此进行更新,它工作正常。如果将来有人需要,这是我的云功能代码,用于更新运动员姓名:
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);
}
});
推荐阅读
- google-sheets - 已解决:查询一个单元格并在另一个单元格中将多个结果作为字符串返回
- python-3.x - 在 GeoDataFrame 中使用网格单元
- php - 强制 php 从不同位置加载 libcurl.so.4?
- flutter - 自动显示 CupertinoSearchTextField() 的键盘 - Flutter
- java - Java 通过 SSH 连接执行命令
- apache-spark - 如何减少 Spark SQL 执行计划的总时间处理
- java - Mockito - 如果在另一个函数调用的函数中设置参数,如何进行单元测试
- python - 使用 Selenium 获取元素属性时遇到问题 | Python
- linux - 在 ubuntu 20.04 上为 32 位交叉编译 poco 失败,说它需要一个共享的 glibc
- python - 按列表选择列(并删除第一行)