MongoDB realize questionnaire / test design

MongoDB features

MongoDB is a document-oriented database that stores. In MongoDB, a record called the document (document), the key is similar to JSON structure of the composition.MongoDB data format

Due to similar characteristics of MongoDB JSON directly stored, MongoDB natural for media data storage structure as a complex structure. This is similar to the questionnaire and examination requirements, implement a relational database mysql this is too complicated and inefficient; but if you use MongoDB to come true, you will find very clear simple.

demand analysis

In a paper, there will be a lot of questions, types of questions can be divided into multiple choice, multiple choice, true questions, short answer and so on. Each question and there will be a lot of options, the options can be a text description and picture may be or graphic combination.

problem

Then the piece of paper in the JSON format should in general be so long:

JSON

Of course, this is only the most simple data structure, to complete the piece of paper, you need to add more attributes.

Design

We use a bottom-up structure design approach, first data structure for each option design.

Design Options

public class Option {
    /**
     * 选项类型
     */
    private Integer oType = 1;

    /**
     * 选项内容
     */
    private String text;
    
    /**
     * 选项图片
     */
    private String img;

    /**
     * 是否正确答案
     */
    private Boolean right;

    /**
     * 用户是否选择
     */
    private Boolean selected;
    ...复制代码

Option type oTypeused to mark option is plain text or pictures or graphics; rightused to mark this option is the correct answer for automatic grading; selectedused to mark the user has not selected this answer.

Design issues

public class Question extends MongoBean {

    /**
     * 数据的id
     */
    private String dataId;

    /**
     * 题目类型,1判断题;2单选题;3多选题
     */
    private Integer qType;

    /**
     * 题目标题
     */
    private String title;

    /**
     * 题目选项
     */
    private List<Option> options;
    /**
     * 数据类型
     * @see rmjk.enums.BizTypeEnum
     */
    private Integer dataType;

    /**
     * 数据标题
     */
    private String dataTitle;
    /**
     * 解析
     */
    private String analysis;

    /**
     * 这题是否答对
     */
    private Boolean right;

    /**
     * 这题答的时长
     */
    private Long duration;

    /**
     * 这题的得分
     */
    private Long points;
    ...复制代码

dataIdThe problem with this for a business data binding, dataTypeused to mark this type of business data, expand both fields facilitate data; dataTitleis the title of business data; optionsis an option to this problem; analysisresolve problems for users answer self-examination after the end; rightit used to record the issue of right or wrong.

New issues

Upper interface

Provide new interface issues:

@PostMapping("/saveOrUpdateQuestion")
public JsonData saveOrUpdateQuestion(@RequestBody Question data) {
    questionService.saveOrUpdateQuestion(data);
    return JsonData.success();
}复制代码

QuestionService:

public void saveOrUpdateQuestion(Question data) {
    if (StringUtils.isEmpty(data.getId())) {// 新增
        writer.insert(manager.getExamDataBase(), ExamConstant.QUESTION_COLLECT, data);
    } else {//修改
        writer.updateDocument(data, ExamConstant.QUESTION_COLLECT);
    }
}复制代码

DAO

Writer:

public void insert(String dataBase, String collect, MongoBean data) {
    if (data.getId() == null) {
        data.setId(BsonTool.uuid());
    }
    MongoCollection<Document> collection = getCollection(dataBase, collect);
    collection.insertOne(Document.parse(JSONObject.toJSONString(data)));
}

public Document updateDocument(MongoBean data, String questionCollect) {
    Document filter = new Document();
    filter.put("id", data.getId());
    Document res = new Document();
    res.put("$set", BsonDocument.parse(JSONObject.toJSONString(data)));
    update(manager.getExamDataBase(), questionCollect, filter, res);
    return res;
}
public boolean update(String dataBase, String collect, Bson filter, Bson update) {
    MongoCollection<Document> collection = getCollection(dataBase, collect);
    UpdateResult ur = collection.updateOne(filter, update);
    return ur.getModifiedCount() > 0;
}复制代码

Such work completed on the back end, the next is how the front end to the back end of the structure to provide such data.

Distal Data Structure

Front-end vueto achieve the JSON structure:

<Modal title="问题编辑" v-model="showEdit" :closable="false" :mask-closable="false">
    <Form ref="question" :model="question" :rules="ruleValidate">
        <FormItem label="题目类型:" prop="qType">
            <Select v-model="question.qType" class="input-180" placeholder="题目类型" @on-change="changeQType(question)">
                <Option v-for="d in qTypes" :value="d.value" :key="d.value">{{ d.label }}</Option>
            </Select>
        </FormItem>
        <FormItem label="题目:" prop="title">
            <Input
                   class="input-95-per"
                   v-model="question.title"
                   type="textarea"
                   row="1"
                   placeholder="题目"
                   ></Input>
        </FormItem>
        <FormItem label="选项:">
            <div v-for="(o, i2) in question.options" :key="i2" style="display:flex">
                <Input class="input-95-per margin-bot-8 margin-right-10" v-model="o.text">
                    <span slot="prepend">{{i2+1}}:</span>
                </Input>
                <Button size="small" @click="addOpt(question)" v-if="i2===0">+</Button>
                <Button size="small" @click="delOpt(question, o)" v-if="i2">-</Button>
                <Checkbox v-model="o.right">正确答案</Checkbox>
            </div>
        </FormItem>
        <FormItem label="答案解析:">
            <Input
                   class="input-95-per"
                   v-model="question.analysis"
                   type="textarea"
                   row="1"
                   placeholder="答案解析"
                   ></Input>
        </FormItem>
    </Form>
    <div slot="footer">
        <Button type="text" @click="cancelQuestion">取消</Button>
        <Button type="primary" :loading="saveLoading" @click="saveQuestion">保存</Button>
    </div>
</Modal>复制代码

Here binding questionis a problem. The piece of paper is a number of issues, coupled with the additional property papers constituted.

question

In questiondataId just on paper can be binding on the id

Exam exam = new Exam();
List<Question> questions = reader.findRandom(manager.getExamDataBase(), ExamConstant.QUESTION_COLLECT, new Document(), Question.class, no);
exam.setTitle(title);
exam.setDuration(dutation);
return exam;复制代码

fileLike turn commentary is the greatest encouragementNo public attention, more surprises waiting for you

Guess you like

Origin juejin.im/post/5db995dcf265da4d2125d04f