mongodb 可以使用MongoDB制作关系数据库吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8129557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can one make a relational database using MongoDB?
提问by ?lker Da?l?
I am going to make a student management system using MongoDB. I will have one table for students and another for attendance records. Can I have a key in the attendance table to reach the students table, as pictured below? How?
我将使用 MongoDB 制作一个学生管理系统。我将为学生准备一张桌子,另一张用于记录出勤记录。我可以在考勤表中使用钥匙到达学生表吗,如下图所示?如何?
回答by Mike Christensen
The idea behind MongoDB is to eliminate (or at least minimize) relational data. Have you considered just embedding the attendance data directly into each student record? This is actually the preferred design pattern for MongoDB and can result in much better performance and scalability.
MongoDB 背后的想法是消除(或至少最小化)关系数据。您是否考虑过将考勤数据直接嵌入到每个学生记录中?这实际上是 MongoDB 的首选设计模式,可以带来更好的性能和可扩展性。
If you truly need highly relational and normalized data, you might want to reconsider using MongoDB.
如果您确实需要高度相关和规范化的数据,您可能需要重新考虑使用 MongoDB。
回答by seanhodges
The answer depends on how you intend to use the data. You really have 2 options, embed the attendance table, or link it. More on these approaches is detailed here: http://www.mongodb.org/display/DOCS/Schema+Design
答案取决于您打算如何使用数据。您真的有 2 个选项,嵌入考勤表或链接它。此处详细介绍了这些方法:http: //www.mongodb.org/display/DOCS/Schema+Design
For the common use-case, you would probably embed this particular collection, so each student record would have an embedded "attendance" table. This would work because attendance records are unlikely to be shared between students, and retrieving the attendance data is likely to require the student information as well. Retrieving the attendance data would be as simple as:
对于常见用例,您可能会嵌入此特定集合,因此每个学生记录都会有一个嵌入的“出勤”表。这是可行的,因为出勤记录不太可能在学生之间共享,而且检索出勤数据可能也需要学生信息。检索出勤数据将非常简单:
db.student.find( { login : "sean" } )
{
login : "sean",
first : "Sean",
last : "Hodges",
attendance : [
{ class : "Maths", when : Date("2011-09-19T04:00:10.112Z") },
{ class : "Science", when : Date("2011-09-20T14:36:06.958Z") }
]
}
回答by Prince Owen
Yes. There are no hard and fast rules. You have to look at the pros and cons of either embedding or referencing data. This video will definitely help (https://www.youtube.com/watch?v=-o_VGpJP-Q0&t=21s). In your example, the phone number attribute should be on the same table (in a document database), because the phone number of a person rarely changes.
是的。没有硬性规定。您必须查看嵌入或引用数据的优缺点。该视频肯定会有所帮助(https://www.youtube.com/watch?v=-o_VGpJP-Q0&t=21s)。在您的示例中,电话号码属性应该在同一个表中(在文档数据库中),因为一个人的电话号码很少更改。