search

Home  >  Q&A  >  body text

怎么用mongodb或者mongoose库建立文档引用

比如:一名学生(学号,名字,性别,年龄)教师(教工号,名字)选课信息(学号,课程号,教工号,成绩)

请问怎么用mongodb模块建立这三个的关系??

習慣沉默習慣沉默2792 days ago597

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-04-28 09:06:59

    MongoDB creates index: db.collection.createIndex( { name: 1 } )
    db - is a database
    collection - is a table (relative to mysql)

    About database establishment, I will write like this: (for reference only)
    student
    {
    _id:ObjectID(<MongoDB ID>),
    id:<student ID>,
    gender: <gender>,
    age: <age>,
    name: <name>,
    course_ids: [

      course_id1:{
         mark: <成绩>,
         teacher_id: <教工号> 
      },
      course_id2:{
         mark: <成绩> 
      }

    ]
    }

    teacher
    {
    _id:ObjectID(<MongoDB ID>),
    id:<faculty ID>,
    name:<name>,
    courses:[ course_id1, course_id2 ]
    }

    course
    {
    _id:ObjectID(<MongoDB ID>),
    id:<course number>
    }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-28 09:06:59

    Mongodb has DBRef as a document reference, which can record the document location of other collections. However, this reference is not the concept of a foreign key in the SQL database. It can only record the collection name and ID number of the referenced document. No other operations can be attached, and it cannot even perform some operations when the referenced document changes or is deleted. All mongodb establishment of reference relationships is mainly done through programs rather than databases.

    reply
    0
  • Cancelreply