【问题标题】:Looking for MongoDB schema suggestions寻找 MongoDB 架构建议
【发布时间】: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

我很好奇你会如何解决这个问题。

如果有点啰嗦,请见谅! (+ 希望这些例子有意义)

【问题讨论】:

    标签: mongodb mongoose nosql


    【解决方案1】:

    您需要有LessonSchema中的用户参考,以便您可以跟踪用户上过多少课。

    const LessonSchema = new mongoose.Schema({
        user:{type:mongoose.Schema.Types.ObjectId,ref:'UserSchema',required:true},
        title: String,
        description: String,
        totalPoints: Number,
        totalWords: Number,
        words: [
          { 
            id: Number,
            word: String,
            points: Number
          }
        ]
    })
    

    并且您需要在LessonResultsSchema 中维护FLAG 以跟踪用户是否已完成该课程。课程完成后,您需要更新pointsEarned,以便获得更新后的积分数据。

    const LessonResultsSchema = new mongoose.Schema({
      user_id: Schema.Types.ObjectId,
      completed:{type:Boolean, default:0},
      results: [
        {
          id: Number,
          lesson_id: Schema.Types.ObjectId,
          pointsEarned: Number,
          wordsLearned: Number,
          wordsToPractice: [
            {
              id: Number,
              correct: Number,
              incorrect: Number,
              bookmarked: Boolean,
            },
          ],
        },
      ],
    });
    
    
    

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多