Alex Rivera | Logout

Can one make a relational database using MongoDB?

Asked 2011-11-14T23:14:40.680
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

Edit
Report

1 Answer

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") }
  ]
}
answered 2011-11-14T23:35:12.980

Your Answer