【发布时间】:2020-08-07 11:28:05
【问题描述】:
我对 MongoDB 比较陌生,我想知道在我的数据库中处理以下类型数据的 “最佳” 解决方案是什么。
收藏:
用户
课程
概述:
注意:认为实现类似于 Duolingo 或其他一些学习网站。
用户拥有所有标准的通用用户信息但他们还需要跟踪他们对每个用户拥有的分数、所学单词、完成百分比等鉴于他们已经开始了课程。
课程包含以下数据:学习的单词、总分、要复习的单词等。
图片
用户在查看他们已经开始的课程时会在小部件中看到的示例...
那么有什么好的方法来处理这个问题呢?我觉得我至少需要一个用户和课程的集合,但是我应该在哪里存储与他们所学课程相关的用户特定数据?
到目前为止我的想法......
// user collection
const UserSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
firstName: { type: String },
lastName: { type: String },
gender: { type: String, enum: ['male', 'female', 'other'] },
admin: Boolean,
address: {
street: String,
city: String,
state: {
type: String,
uppercase: true,
required: true,
enum: statesArray,
},
zip: Number,
},
});
// lesson_results collection
const LessonResultsSchema = new mongoose.Schema({
user_id: Schema.Types.ObjectId,
results: [
{
id: Number,
lesson_id: Schema.Types.ObjectId,
pointsEarned: Number,
wordsLearned: Number,
wordsToPractice: [
{
id: Number,
correct: Number,
incorrect: Number,
bookmarked: Boolean,
},
],
},
],
});
// lesson collection
const LessonSchema = new mongoose.Schema({
title: String,
description: String,
totalPoints: Number,
totalWords: Number,
words: [
{
id: Number,
word: String,
points: Number
}
]
})
// if logged in
// find user in user collection
// find user's results in lessonsResults collection by using user_id
// iterate over each result to find their lesson & lesson's data
// combine data and send to frontend
// calculate percentage complete + other calculations
// render aggregate data on screen
我很好奇你会如何解决这个问题。
如果有点啰嗦,请见谅! (+ 希望这些例子有意义)
【问题讨论】: