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.
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.