mongodb - mogodb如何进行设计
淡淡烟草味
淡淡烟草味 2017-04-28 09:04:48
0
1
742

想知道大家在做非关系型数据库的项目时是如何进行设计的,以学生选课为例,每个学生可以选择多个课程,每个课程可以让多个学生选择,这种多对多关系要如何设计,如果我要删除一个课程,如何保证数据库的一致性?

淡淡烟草味
淡淡烟草味

reply all(1)
仅有的幸福

MongoDB considers two basic modes: document and table.

The document mode records a record into a document. Taking a student's course selection as an example, it records all the courses he has chosen under a student's document; the split-table mode is like a relational database, where students and courses are separated into separate tables, and then Create another table showing the correspondence between students and courses (course selection).

The main advantage of documents is high reading efficiency. MongoDB does not have joint. If it is divided into tables, each query involves several tables and requires several queries. The main disadvantage of the document is that it is inconvenient to maintain. For example, if you modify the information of a course, you have to find all the student documents containing this course and modify them one by one. In addition, the size of a document is limited and data cannot be added indefinitely.

The advantages and disadvantages of sub-table are opposite to those of document type.

In practice, the choice must be weighed. Data that is mainly read tends to choose the document mode, and data with high write/modification frequency can consider splitting tables. In addition, for fields that may increase data infinitely, it is usually necessary to consider storage in separate tables.

Usually, the two are used in combination, and the information with higher reading frequency is recorded in the document, and other detailed information is created separately. In your example, two tables are created for students and courses. The basic information of course selection (such as course code and name) is recorded under each student document, and then the specific information of each course is stored in the course table.

Whenever you list a student's course selection information, you can list the codes and names of all the course selections in one query. When you need to view the detailed information of a specific course, do another query on the course schedule. This can save one query compared to complete table partitioning, and reduces the size of the document and the scope of modification when modifying course information compared to complete document recording.

If you want to delete a course, first delete the document of this course in the course schedule, then find all the records containing this course in the student table and modify it. Flexible use of indexes and operators is not a problem.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!