Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql到mongodb数据库及查询转换_Mongodb - Fatal编程技术网

mysql到mongodb数据库及查询转换

mysql到mongodb数据库及查询转换,mongodb,Mongodb,MySQL数据库中定义的以下结构 我有一个学位表包含两列:学位id、学位名称 2.分行另一张表 分支机构id、分支机构名称、学位id 3.主题表 学科id、学科名称、学科代码、学位id、学期 4.教学大纲 现在,我可以使用mysql轻松获取分词学位、分支、学期的教学大纲,但我是mongo db的新手,请帮助在MongoDb中,您必须将概念更一般化,并包含其中的所有关系 您的数据库Mysql,在mongodb上看起来像这样: degree{ _id : ...,

MySQL数据库中定义的以下结构

  • 我有一个学位表
    包含两列:学位id、学位名称
    2.分行另一张表
    分支机构id、分支机构名称、学位id
    3.主题表
    学科id、学科名称、学科代码、学位id、学期
    4.教学大纲

  • 现在,我可以使用mysql轻松获取分词学位、分支、学期的教学大纲,但我是mongo db的新手,请帮助在
    MongoDb
    中,您必须将概念更一般化,并包含其中的所有关系

    您的数据库
    Mysql
    ,在
    mongodb
    上看起来像这样:

         degree{
                _id : ...,
                name : ...,
                branches :[ {
                             _id: ...
                             name: ....,
                             syllabus : [ {
                                           _id: ...,
                                           name: ....,
                                           sort_note: ....
                                           attachment:...
                                           semetster,... 
                                          } , ..   ],...{
                           } ],
                 subjects: [ {
                              _id: ..,
                              name: ....,
                              code: .....,
                              semester: ....
                           }, ...]
                 }
    
    如果id_分支和id_度是唯一的,就像containt是对象一样,那么其他的是数组


    关于

    我查看了您的SQL数据模型,但对我来说有些不太清楚。 似乎你把学位加入了每一个集合(分支/学科/教学大纲)

    我猜你最终会在最高级别的学位id(分支id/学科id)上查询你的教学大纲

    select * from syllabus where degree_id = ?
    

    这意味着你不需要像这样通过在分支或学科上设置的学位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 = ?
    
    [
       {
       _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'})
    
    [
       {
       _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'),
       }
    ]
    
    syllabus.find({degree:ObjectId('degree_id_1')})
    
    所以,假设您确实通过为教学大纲记录设置的学位id来搜索教学大纲集合。 这使得在MongoDB中更容易获得相同的结果

    将当前SQL模型转换为MongoDB模型的最简单方法是使用4个集合而不是4个表

    学位

    {
       degree_id : ...
       degree_name : ...
    }
    
    分支机构

    {
       branch_id : ...
       branch_name : ...
       degree : {
          degree_id : ...
          degree_name : ...
       }
    }
    
    科目也是如此,教学大纲中嵌入学位、分支和科目文档的方式最终也是如此。 MongoDB不需要固定关系或外键,也没有关系数据库中的连接。因此,要么嵌入其他集合,要么通过_id字段添加对文档的引用(就像SQL一样),这取决于最终的查询结果

    嵌入教学大纲集合中的其他集合如下所示

    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 = ?
    
    [
       {
       _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'})
    
    [
       {
       _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'),
       }
    ]
    
    syllabus.find({degree:ObjectId('degree_id_1')})
    
    这样你就可以像这样查询你的教学大纲了

    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 = ?
    
    [
       {
       _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'})
    
    [
       {
       _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'),
       }
    ]
    
    syllabus.find({degree:ObjectId('degree_id_1')})
    

    或者您只使用_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 = ?
    
    [
       {
       _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'})
    
    [
       {
       _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'),
       }
    ]
    
    syllabus.find({degree:ObjectId('degree_id_1')})
    
    您只能通过以下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 = ?
    
    [
       {
       _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'})
    
    [
       {
       _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'),
       }
    ]
    
    syllabus.find({degree:ObjectId('degree_id_1')})
    

    我希望您理解这个概念,并决定哪种方法更适合您的应用程序。这完全取决于您的教学大纲文档在查询结果中的外观

    在我的应用程序中,我总是先考虑查询,然后对文档建模

    您应该始终记住,您没有连接,有一些插件可以为您提供某种连接和预定义的模型布局,如Mongoose。但在我看来,这与MongoDB完全背道而驰。尝试以MongoDB方式对数据建模:-)


    如果有不清楚的地方,请告诉我。

    如果您能描述实体之间的一对一和一对多关系,那就更好了。例如,我猜教学大纲和学科之间有一对一的关系,一个分支会有许多学科。但我不确定学位和分支的关系。一个分支机构可以拥有多个学位,或者反之亦然?问题是从2013年开始,您仍然在使用m8 lel