32

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?

diagram of a relational database

Community
  • 1
  • 1
İlker Dağlı
  • 1,593
  • 3
  • 13
  • 10

3 Answers3

45

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.

If you truly need highly relational and normalized data, you might want to reconsider using MongoDB.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • 2
    If you're new to MongoDB, there's a pretty cool little tutorial on their website: http://www.mongodb.org/ (just click on "Try it out" and then type `tutorial` in the shell.) – Mike Christensen Nov 14 '11 at 23:36
  • 1
    @MikeChristensen At this time, this advice is no longer valid. They probably changed their website. – Kunok Sep 01 '16 at 22:53
  • 4
    What if you started database with embedded attendance, but then you need to show attendance per class, it looks like it will be very inefficient to traverse all users and pull attendance data for specific day and class? – Vedmant Sep 09 '17 at 21:25
  • I'm not a Mongo expert, but the way I understand, Mongo will create indexes on those types of queries so it doesn't have to traverse all the individual users. Same thing SQL would do, only the data is more spread out. – Mike Christensen Oct 29 '17 at 16:58
  • 5
    I'm confused as to why this answer is highly ranked. If you need to justify the question before you answer it, this is a trivial task; there are many cases where one developer inherits code from the company, they do not have the power to change the entire code's architecture, and they need to make the code do something new to satisfy the requirements of another team that don't use your specific architecture. Now that that's out of the way, we're left without an attempt to answer the question. – Jessica Pennell Jun 20 '19 at 16:38
  • That said it is easier to complain than to put myself in the arena. This article does an excellent job not only explaining what Mike did - which is important - but also explaining 2 ways to do relational databases if you really have to, and the caveats to each approach. http://www.sarahmei.com/blog/2013/11/11/why-you-should-never-use-mongodb/ To sum the article up : you either use IDs and join documents programmatically at the application layer, or you duplicate data. – Jessica Pennell Jun 20 '19 at 16:39
23

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

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") }
  ]
}
seanhodges
  • 17,426
  • 15
  • 71
  • 93
1

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.

Prince Owen
  • 1,225
  • 12
  • 20
  • 3
    Link is now dead – Mike Jul 22 '20 at 22:06
  • https://www.youtube.com/watch?v=9JZJsChpwKs new link for the video: Title: "MongoDB Tutorial #15 - Relational Data" so someone could search if the link becomes stale again. – John Dec 23 '20 at 01:15