Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of questionnaire / examination Design by using MongoDB

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how to use MongoDB to achieve the questionnaire / test design function, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Characteristics of MongoDB

MongoDB is a database for document storage. In MongoDB, a record is called document (document), which consists of key-value pairs similar to the JSON structure.

Because of its characteristics similar to MongoDB's direct storage of JSON, MongoDB is naturally suitable as a medium for storing data structures with complex structures. Similar to the requirements of questionnaires and examinations, the implementation of a relational database such as mysql is too complex and inefficient, while if you use MongoDB to implement it, you will find it extremely clear and simple.

Demand analysis

In a test paper, there will be many questions. Generally speaking, the types of questions can be divided into single-choice questions, multiple-choice questions, judgment questions, short answer questions and so on. There are many options for each question, either a text description, a picture or a combination of picture and text.

Then the JSON format of a test paper should look roughly like this:

Of course, this is only the simplest data structure, to complete a test paper, you need to add more attributes.

Structural design.

We adopt a bottom-up structural design approach, and first design the data structure of each option.

Option design

Public class Option {/ * option type * / private Integer oType = 1; / * option content * / private String text; / * option picture * / private String img; / * * whether the answer is correct * / private Boolean right; / * whether the user chooses * / private Boolean selected;...

The option type oType is used to indicate whether the option is plain text or picture or text; right is used to indicate whether this option is the correct answer and is used to automatically grade the paper; and selected is used to indicate whether the user has selected this answer.

Problem design

Public class Question extends MongoBean {/ * id * / private String dataId; / * question types, 1 judging questions, 2 single-choice questions 3 multiple choice questions * / private Integer qType; / * * title * / private String title; / * * topic options * / private List options; / * data type * @ see rmjk.enums.BizTypeEnum * / private Integer dataType; / * data title * / private String dataTitle; / * * parse * / private String analysis; / * * whether the question is answered correctly * / private Boolean right / / private Long duration; / * the score of this question * / private Long points;.

DataId is used to bind this question to the same business data, and dataType is used to mark the type of business data. These two fields facilitate data expansion; dataTitle is the title of business data; options is the option for this question; analysis question resolution is used for self-examination after users have answered the question; and right is used to record whether the question is correct or not.

New question

Upper interface

Provide an interface for new questions:

@ 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 () {/ / add writer.insert (manager.getExamDataBase (), ExamConstant.QUESTION_COLLECT, data);} else {/ / modify 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 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 collection = getCollection (dataBase, collect); UpdateResult ur = collection.updateOne (filter, update); return ur.getModifiedCount () > 0;}

In this way, the back-end work is all done, and the next step is how the front-end provides such a data structure to the back-end.

The front end implements the data structure.

The front end uses vue to implement the construction of JSON:

{{d.label}}: +-correct answer to cancel saving

The question bound here is a problem. On the other hand, a test paper is made up of multiple questions, plus the additional attributes of the test paper.

The dataId on question just happens to bind the id of the test paper.

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

The above is how to use MongoDB to achieve the questionnaire / test design function, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report