MongoDB實現(xiàn)問卷/考試設(shè)計功能
MongoDB的特點
MongoDB是一個面向文檔存儲的數(shù)據(jù)庫。在MongoDB中,一條記錄叫做document(文檔),由類似于JSON結(jié)構(gòu)的鍵值對組成。
由于類似于MongoDB直接存儲JSON的特性,MongoDB天生適合作為存儲結(jié)構(gòu)復(fù)雜的數(shù)據(jù)結(jié)構(gòu)的介質(zhì)。類似于問卷調(diào)查和考試這種需求,用mysql這種關(guān)系型數(shù)據(jù)庫實現(xiàn)起來太過復(fù)雜,效率低下;而如果使用MongoDB來實現(xiàn)的話,則會發(fā)現(xiàn)異常清晰簡單。
需求分析
在一張試卷中,會有很多個問題,問題的類型大體上可以分為單選題、多選題、判斷題、簡答題等。每一個問題又會有很多個選項,選項可以是文字描述也可以是圖片又或者圖文結(jié)合。
那么一張試卷的JSON格式應(yīng)該大體上長成這樣:
當(dāng)然這只是最簡單的數(shù)據(jù)結(jié)構(gòu),要完成一張試卷,還需要加入更多的屬性。
結(jié)構(gòu)設(shè)計
我們采用自底向上的結(jié)構(gòu)設(shè)計方式,先對每個選項的數(shù)據(jù)結(jié)構(gòu)進行設(shè)計。
選項設(shè)計
public class Option { /** * 選項類型 */ private Integer oType = 1; /** * 選項內(nèi)容 */ private String text; /** * 選項圖片 */ private String img; /** * 是否正確答案 */ private Boolean right; /** * 用戶是否選擇 */ private Boolean selected; ...
選項類型 oType
用來標(biāo)志選項是普通文本還是圖片或者圖文; right
用來標(biāo)志這個選項是否是正確答案,用于自動判卷; selected
用來標(biāo)志用戶有沒有選擇這個答案。
問題設(shè)計
public class Question extends MongoBean { /** * 數(shù)據(jù)的id */ private String dataId; /** * 題目類型,1判斷題;2單選題;3多選題 */ private Integer qType; /** * 題目標(biāo)題 */ private String title; /** * 題目選項 */ private List<Option> options; /** * 數(shù)據(jù)類型 * @see rmjk.enums.BizTypeEnum */ private Integer dataType; /** * 數(shù)據(jù)標(biāo)題 */ private String dataTitle; /** * 解析 */ private String analysis; /** * 這題是否答對 */ private Boolean right; /** * 這題答的時長 */ private Long duration; /** * 這題的得分 */ private Long points; ...
dataId 用于將這個問題同一個業(yè)務(wù)數(shù)據(jù)綁定, dataType 用來標(biāo)志這個業(yè)務(wù)數(shù)據(jù)的類型,這兩個字段方便數(shù)據(jù)的擴展; dataTitle 是業(yè)務(wù)數(shù)據(jù)的標(biāo)題; options 是這個問題的選項; 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()); } 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; }
這樣后端的工作就全部完成了,接下來就是前端怎么給后端提供這樣的數(shù)據(jù)結(jié)構(gòu)了。
前端實現(xiàn)數(shù)據(jù)結(jié)構(gòu)
前端使用 vue 實現(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="選項:"> <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òu)成的。
在 question 上的dataId剛好就能綁定上試卷的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;
總結(jié)
以上所述是小編給大家介紹的MongoDB實現(xiàn)問卷/考試設(shè)計功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
mongodb權(quán)限設(shè)置之添加管理員、普通用戶的方法
這篇文章主要介紹了mongodb添加管理員、普通用戶的方法,同時介紹了mongodb開啟權(quán)限認(rèn)證后PHP客戶端的兩種連接方法,需要的朋友可以參考下2014-06-06MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實例
這篇文章主要介紹了MongoDB 導(dǎo)出導(dǎo)入備份恢復(fù)數(shù)據(jù)詳解及實例的相關(guān)資料,需要的朋友可以參考下2016-10-10分布式文檔存儲數(shù)據(jù)庫之MongoDB備份與恢復(fù)的實踐詳解
這篇文章主要介紹了分布式文檔存儲數(shù)據(jù)庫之MongoDB備份與恢復(fù),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Linux服務(wù)器下MariaDB 10自動化安裝部署
這篇文章主要介紹了Linux服務(wù)器下MariaDB 10自動化安裝部署,需要的朋友可以參考下2016-08-08Ubuntu 14.04 安裝 MongoDB 及 PHP MongoDB Driver詳細介紹
這篇文章主要介紹了Ubuntu 14.04 安裝 MongoDB 及 PHP MongoDB Driver詳細介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10