濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能

MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能

熱門標(biāo)簽:外呼線路資源屬于電信業(yè)務(wù)嗎 crm外呼系統(tǒng)聯(lián)系方式 呼和浩特外呼系統(tǒng)原理是什么 智能外呼系統(tǒng)官網(wǎng) 青白江400企業(yè)電話申請(qǐng) 河南電話外呼系統(tǒng)招商 內(nèi)蒙古營銷智能外呼系統(tǒng)哪個(gè)好 小裙科技電銷機(jī)器人怎樣 長沙電銷外呼防封卡是什么

MongoDB的特點(diǎn)

MongoDB是一個(gè)面向文檔存儲(chǔ)的數(shù)據(jù)庫。在MongoDB中,一條記錄叫做document(文檔),由類似于JSON結(jié)構(gòu)的鍵值對(duì)組成。 

由于類似于MongoDB直接存儲(chǔ)JSON的特性,MongoDB天生適合作為存儲(chǔ)結(jié)構(gòu)復(fù)雜的數(shù)據(jù)結(jié)構(gòu)的介質(zhì)。類似于問卷調(diào)查和考試這種需求,用mysql這種關(guān)系型數(shù)據(jù)庫實(shí)現(xiàn)起來太過復(fù)雜,效率低下;而如果使用MongoDB來實(shí)現(xiàn)的話,則會(huì)發(fā)現(xiàn)異常清晰簡單。

需求分析

在一張?jiān)嚲碇?,?huì)有很多個(gè)問題,問題的類型大體上可以分為單選題、多選題、判斷題、簡答題等。每一個(gè)問題又會(huì)有很多個(gè)選項(xiàng),選項(xiàng)可以是文字描述也可以是圖片又或者圖文結(jié)合。

那么一張?jiān)嚲淼腏SON格式應(yīng)該大體上長成這樣:

當(dāng)然這只是最簡單的數(shù)據(jù)結(jié)構(gòu),要完成一張?jiān)嚲?,還需要加入更多的屬性。

結(jié)構(gòu)設(shè)計(jì)

我們采用自底向上的結(jié)構(gòu)設(shè)計(jì)方式,先對(duì)每個(gè)選項(xiàng)的數(shù)據(jù)結(jié)構(gòu)進(jìn)行設(shè)計(jì)。

選項(xiàng)設(shè)計(jì)

public class Option {
 /**
  * 選項(xiàng)類型
  */
 private Integer oType = 1;

 /**
  * 選項(xiàng)內(nèi)容
  */
 private String text;
 
 /**
  * 選項(xiàng)圖片
  */
 private String img;

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

 /**
  * 用戶是否選擇
  */
 private Boolean selected;
 ...

選項(xiàng)類型 oType 用來標(biāo)志選項(xiàng)是普通文本還是圖片或者圖文; right 用來標(biāo)志這個(gè)選項(xiàng)是否是正確答案,用于自動(dòng)判卷; selected 用來標(biāo)志用戶有沒有選擇這個(gè)答案。

問題設(shè)計(jì)

public class Question extends MongoBean {
 /**
  * 數(shù)據(jù)的id
  */
 private String dataId;
 /**
  * 題目類型,1判斷題;2單選題;3多選題
  */
 private Integer qType;
 /**
  * 題目標(biāo)題
  */
 private String title;
 /**
  * 題目選項(xiàng)
  */
 private ListOption> options;
 /**
  * 數(shù)據(jù)類型
  * @see rmjk.enums.BizTypeEnum
  */
 private Integer dataType;
 /**
  * 數(shù)據(jù)標(biāo)題
  */
 private String dataTitle;
 /**
  * 解析
  */
 private String analysis;
 /**
  * 這題是否答對(duì)
  */
 private Boolean right;
 /**
  * 這題答的時(shí)長
  */
 private Long duration;
 /**
  * 這題的得分
  */
 private Long points;
 ...

dataId 用于將這個(gè)問題同一個(gè)業(yè)務(wù)數(shù)據(jù)綁定, dataType 用來標(biāo)志這個(gè)業(yè)務(wù)數(shù)據(jù)的類型,這兩個(gè)字段方便數(shù)據(jù)的擴(kuò)展; dataTitle 是業(yè)務(wù)數(shù)據(jù)的標(biāo)題; options 是這個(gè)問題的選項(xiàng); analysis 問題的解析,用于用戶答題結(jié)束后的自查; right 用來記錄問題的正確與否。

新增問題

上層接口

提供新增問題的接口:

@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());
 }
 MongoCollectionDocument> 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) {
 MongoCollectionDocument> collection = getCollection(dataBase, collect);
 UpdateResult ur = collection.updateOne(filter, update);
 return ur.getModifiedCount() > 0;
}

這樣后端的工作就全部完成了,接下來就是前端怎么給后端提供這樣的數(shù)據(jù)結(jié)構(gòu)了。

前端實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)

前端使用 vue 實(shí)現(xiàn)JSON的構(gòu)造:

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="選項(xiàng):">
   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>

這里綁定的 question 就是一個(gè)問題了。而一張?jiān)嚲韯t是由多個(gè)問題,再加上試卷的額外屬性構(gòu)成的。

在 question 上的dataId剛好就能綁定上試卷的id

Exam exam = new Exam();
ListQuestion> questions = reader.findRandom(manager.getExamDataBase(), ExamConstant.QUESTION_COLLECT, new Document(), Question.class, no);
exam.setTitle(title);
exam.setDuration(dutation);
return exam;

總結(jié)

以上所述是小編給大家介紹的MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

您可能感興趣的文章:
  • MySQL和MongoDB設(shè)計(jì)實(shí)例對(duì)比分析
  • MongoDB進(jìn)階之動(dòng)態(tài)字段設(shè)計(jì)詳解
  • MongoDB 數(shù)據(jù)庫的命名、設(shè)計(jì)規(guī)范詳解
  • windows7下使用MongoDB實(shí)現(xiàn)倉儲(chǔ)設(shè)計(jì)
  • MongoDB設(shè)計(jì)方法以及技巧示例詳解

標(biāo)簽:呼倫貝爾 安順 楚雄 白山 菏澤 舟山 池州 黃石

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能》,本文關(guān)鍵詞  MongoDB,實(shí)現(xiàn),問卷,考試,設(shè)計(jì),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于MongoDB實(shí)現(xiàn)問卷/考試設(shè)計(jì)功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    井陉县| 无极县| 宜州市| 蓝田县| 韩城市| 兴义市| 商丘市| 赤壁市| 化德县| 临潭县| 井冈山市| 溧阳市| 龙州县| 荃湾区| 阿拉善左旗| 石阡县| 湖口县| 蓬溪县| 泰和县| 梧州市| 故城县| 伊金霍洛旗| 个旧市| 绥德县| 体育| 天峻县| 乐亭县| 牟定县| 德兴市| 灯塔市| 嘉义市| 临武县| 营口市| 中卫市| 元谋县| 镇江市| 张掖市| 阜新市| 无为县| 吉安县| 利辛县|