Hierarchy relationship in nosql

时间:2019-01-07 13:23:20

标签: mongodb nosql data-modeling

course
  lesson
    activity

About three activities create one lesson and about 16 lessons make up the course.

I want to separate the activity and lesson documents. (to make lessons and courses by selecting them)

Each document has a CRUD operation, and lesson and activity appear when outputting course information.

Which is better for reference and inclusion in nosql?

1 个答案:

答案 0 :(得分:1)

我不确定是否误解了您的问题,但是为每种记录类型(活动,课程和课程)创建单独的集合,然后根据需要简单地引用所需的课程和活动的ID是没有问题的。这会给您层次结构的外观。

例如,您的课程可能有类似以下内容

{
  "_id": {
    "$oid": "5bbf5d8e1ed05050136206ev"
  },
  "course_name": "Course 1",
  "lessons": [
    "_id": {
      "$oid": "5bbf5d8e1ed05050136206ew"
    },
    "_id": {
      "$oid": "5bbf5d8e1ed05050136206ex"
    },
    "_id": {
      "$oid": "5bbf5d8e1ed05050136206ey"
    },
  ]
}

这是一个教训

{
  "_id": {
    "$oid": "5bbf5d8e1ed05050136206ev"
  },
  "lesson_name": "Lesson 1",
  "activity": [
    "_id": {
      "$oid": "5bbf5d8e1ed05050136206ez"
    },
    "_id": {
      "$oid": "5bbf5d8e1ed05050136206ea"
    },
    "_id": {
      "$oid": "5bbf5d8e1ed05050136206eb"
    },
  ]
}

如果您知道要显示的详细信息的种类并希望最大程度地减少查询,那么将某些详细信息包含在多个馆藏中是没有问题的,例如下面的课程

{
  "_id": {
    "$oid": "5bbf5d8e1ed05050136206ev"
  },
  "course_name": "Course 1",
  "lessons": [
    {
      "_id": {
        "$oid": "5bbf5d8e1ed05050136206ew"
      },
      "lesson_name": "Lesson 1"
    },
    {
      "_id": {
        "$oid": "5bbf5d8e1ed05050136206ex"
      },
      "lesson_name": "Lesson 2"
    },
    {
      "_id": {
        "$oid": "5bbf5d8e1ed05050136206ey"
      },
      "lesson_name": "Lesson 3"
    },
  ]
}

希望这会有所帮助。