6

foreword

MongoDB is different from our commonly used mysql. As a non-relational database, data storage has been stored in documents in a format similar to json. There is no external construction, and the association between mysql tables cannot be realized. So, how to implement one-to-one, one-to-many, many-to-one, and many-to-many relationships?

one-to-many

for a class data

 {
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "软件181班",
   "number": "36"
}

with multiple student data

 {
   "_id":ObjectId("52ffc33cd85242f436000002"),
   "name": "张三",
   "num": "185762",
   "address": "河北省XX市"
}
{
   "_id":ObjectId("52ffc33cd85242f436000003"),
   "name": "李四",
   "num": "185763",
   "address": "河北省XX市"
}

There are two methods of embedding and referencing

embedded

 {
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "软件181班",
   "number": "36",
   "student": [
      {
        "name": "张三",
        "num": "185762",
        "address": "河北省XX市"
      },
      {
        "name": "李四",
        "num": "185763",
        "address": "河北省XX市"
      }]
}

The advantage is that the associated data can be found in a single query.
The disadvantage is that as the amount of associated data increases, the read and write time increases.

quote

 {
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "软件181班",
   "number": "36",
   "student_ids": [
      ObjectId("52ffc4a5d85242602e000001"),
      ObjectId("52ffc4a5d85242602e000002")
    ]
}

By way of reference, the basic student data is stored in the student table, and a piece of class data only stores all related student IDs.
When performing an associated query, two queries are required, the first is to query the class information, and the second is to query the student information by using the class-associated student id.
The advantage is to reduce one read and write time when there is a large amount of associated data.
The disadvantage is that it causes multiple queries and increases the query time.

many-to-many

For many-to-many relationships, we need a specific analysis of specific problems.
Such as books and labels many-to-many. Only need to find the corresponding tag of the book, and there is no function of finding books by tag (if there is no). Then you only need to record the book and label data in one table.

 {
   "_id":ObjectId("00000111111"),
   "name": "计算机组成原理",
   "tag": [
      {
        "name": "计算机",
      },
      {
        "name": "考研",
      }]
}
{
   "_id":ObjectId("00000111111"),
   "name": "汤家凤高等数学18讲",
   "tag": [
      {
        "name": "数学",
      },
      {
        "name": "考研",
      }]
}

But in terms of many-to-many courses and classes, we sometimes need to query which courses are in the class, and sometimes we need to query which classes are in this course, but in most cases, we are inquiring which courses are in the class. We can set up a class table, a class table, and associate related courses under the class table.
class table

 {
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "软件181班",
   "number": "36",
   "klass_ids": [
      ObjectId("52ffc4a0001"),
      ObjectId("52ffc4a0002")
    ]
}
{
   "_id":ObjectId("52ffc33cd85242f436000001"),
   "name": "软件182班",
   "number": "35",
   "klass_ids": [
      ObjectId("52ffc4a0001"),
      ObjectId("52ffc4a0003")
    ]
}

Class Schedule

 {
   "_id":ObjectId("52ffc4a0001"),
   "name": "数据结构",
}
{
   "_id":ObjectId("52ffc4a0002"),
   "name": "计算机网络",
}
{
   "_id":ObjectId("52ffc4a0003"),
   "name": "软件工程",
}

For most scenarios, we can query which courses are available by class. For which classes are in the course query, we can also check back through the statement.

Summarize

Compared with Mysql, NoSql is relatively more flexible. We should abandon the idea of mysql and design the database for our scenarios.


小强Zzz
1.2k 声望32 粉丝