Mysql to mongodb and query conversion

The following structure is defined in MySQL db

  • I have one table for the degree and contains two columns: degree_id, degree_name
    2. Another table for the branch
    branch_id, branch_name, degree_id
    3. The object table
    subject_id, subject_name, subject_code, degree_id, semester
    4.syllabus
syllabus_id,syllabus_name,sort_note,attachment,semester,degree_id,branch_id,subject_id

Now I can easily get the curriculum, fork, semester using mysql, but I'm new to mongo db, please help

+4
source share
2 answers

In MongoDbyou, you need to accept the concept of a more general one and include all relationships in it.

Your database Mysqllooks like this MongoDb:

     degree{
            _id : ...,
            name : ...,
            branches :[ {
                         _id: ...
                         name: ....,
                         syllabus : [ {
                                       _id: ...,
                                       name: ....,
                                       sort_note: ....
                                       attachment:...
                                       semetster,... 
                                      } , ..   ],...{
                       } ],
             subjects: [ {
                          _id: ..,
                          name: ....,
                          code: .....,
                          semester: ....
                       }, ...]
             }

id_branch id_degree , , else .

0

SQL - . , (// ).

, , , degree_id (branch_id/subject_id) ,

select * from syllabus where degree_id = ?

select * from syllabus where branch_id = ?

, _, ,

select s.* from syllabus s
join branch b on b.branch_id = s.branch_id
join subject su on su.subject_id = s.subject_id
where b.degree_id = ? // or su.degree_id = ?

, , _, . MongoDB.

SQL MongoDB: 4 4 .

{
   degree_id : ...
   degree_name : ...
}

{
   branch_id : ...
   branch_name : ...
   degree : {
      degree_id : ...
      degree_name : ...
   }
}

, , , , , . MongoDB JOINS, . , , _id ( , SQL), , .

[
   {
   _id : ObjectId('syllabus_id_1'), //_id or syllabus_id but MongoDB adds an _id field automatically,
   syllabus_name : 'syllabus1',
   sort_note : 'a note',
   attachment : 'my attachment',
   semester : 'semester1',
   degree : {
      degree_id : ObjectId('degree_id_1'),
      degree_name : 'degree1'
   },
   branch : {
      branch_id : ObjectId('branch_id_1'),
      branch_name : 'branch1',
      degree : {
         degree_id  : ObjectId('degree_id_1'),
         degree_name : 'degree1'
      }
   },
   subject : {
      subject_id : ObjectId('subject_id_1'),
      subject_name : 'subject1',
      subject_code : 'code',
      semester : 'semester1',
      degree : {
         degree_id  : ObjectId('degree_id_1'),
         degree_name : 'degree1'
      }
   }
   }
]

, , ,

syllabus.find({'degree.degree_id':ObjectId('degree_id_1'), 'degree.degree_name':'degree1'})

syllabus.find({'branch.branch_id':ObjectId('branch_id_1'), 'branch.branch_name':'branch1'})

_id, :

[
   {
   _id : ObjectId('syllabus_id_1'), //or syllabus_id but MongoDB adds an _id field automatically,
   syllabus_name : 'syllabus1',
   sort_note : 'a note',
   attachment : 'my attachment',
   semester : 'semester1',
   degree : ObjectId('degree_id_1'),
   branch : ObjectId('branch_id_1'),
   subject : ObjectId('subject_id_1'),
   }
]

id,

syllabus.find({degree:ObjectId('degree_id_1')})

syllabus.find({degree:ObjectId('branch_id_1')})

, , . , .

, .

, JOINS, , - , Mongoose. , , , MongoDB. MongoDB: -)

, , - .

0

Source: https://habr.com/ru/post/1657585/


All Articles