【问题标题】:Collection schema for a quiz app in MeteorMeteor 中测验应用程序的集合模式
【发布时间】:2016-01-18 14:24:10
【问题描述】:

我正在 Meteor 中创建一个测验应用程序。

问题的架构是什么样的?我正在考虑做类似的事情

const QuestionSchema = new SimpleSchema({
  text: {
    type: String,
  },
  correctAnswers: {
    type: [Object],
  },
  'correctAnswers.$.text': {
    type: String,
  },
  wrongAnswers: {
    type: [Object],
  },
  'wrongAnswers.$.text': {
    type: String,
  },
});

但它真的很聪明吗?如何保存某些用户选择的答案?

【问题讨论】:

    标签: mongodb meteor collections schema


    【解决方案1】:

    正如@gnerkus 建议的那样,为每个答案设置一个correct 布尔值会更简单,但每个答案还需要一个_id 以便您引用它,例如用户选择了哪些答案?

    const AnswerSchema = new SimpleSchema({
      _id: { type: String },
      text: { type: String },
      correct: { type: Boolean }
    });
    
    const QuestionSchema = new SimpleSchema({
      text: { type: String },
      answers: { type: [AnswerSchema] }
    });
    

    【讨论】:

      【解决方案2】:

      您需要为答案定义一个单独的架构。

      var QuestionSchema = new SimpleSchema({
        text: {
          type: String
        },
        answers: [{
          type: SimpleSchema.Types.ObjectId,
          ref: 'AnswerSchema'
        }]
      });
      
      var AnswerSchema = new SimpleSchema({
        text: {
          type: String
        },
        correct: {
          type: Boolean
        },
        question: {
          type: SimpleSchema.Types.ObjectId,
          ref: 'QuestionSchema'
        }
      });
      

      如果您需要跟踪用户选择的答案,则必须将它们存储在用户实例中:

      var UserSchema = new SimpleSchema({
        // Define other attributes here.
        answers: [{
          type: SimpleSchema.Types.ObjectId,
          ref: 'AnswerSchema'
        }]
      });
      

      【讨论】:

      • 但是我怎么知道用户选择了哪个答案呢?
      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 2014-03-20
      • 2017-12-08
      • 2015-09-09
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      相关资源
      最近更新 更多