How do you model many-to-many relationships in MongoDB without using an intermediate table? -


I am trying to create many-to-many relationships with the added requirement in Mongodi, which is "meta School Model {_id: & lt; ObjectID123 & gt ;, Name: "Student" Cityville High School ", Location:" Cityville, Statueville "} Now, I have many relationships with them, so students can be a part of many schools, and there are many students in the school. Usually, mangodibi You could have placed an array of other models in each model, for example:

  Student Model {_id: , Name: "Foo Bar", School: [, & lt; SchoolID2 & gt;, ...]} School Model [[Litter; StudentID1 & gt;, & lt; StudentID2 & gt ;, ...]}   

But what if I have to keep the data on that relationship? For example, when the student entered the school and got out, on that date: "enter_date" and "exit_date" In SQL, you have a This table will look like this:

  Student School: {studentId: 123, schoolId: 456, enter_date: 12-12-2014, exit_date: 12 -12-2015}   

How do you do it in Mangaodbi? In some of the options I have already denied:

  1. Creating a new collection (i.e. student-school collection) that originally copies the SQL Join table. Employed outside because it does not seem to be the right way to use Mongo, management can also be difficult to update / delete.

    How about, in the student model, you just do not put the school ID in the array , But the whole document providing meta-data? Like enter_date, exit_date? Both placements (i.e. students and schools) may be too much for that annotation, but it can be practical even on the basis of desired usage.

      Student model {_id: & lt; ObjectID1 & gt ;, Name: "Foo Bar", School: [[_id: & lt; SchoolID1 & gt ;, enter_date: ..., exit_date: ...}, {_id: & lt; SchoolID2 & gt; enter_date:. .., exit_date: ...}, ...]}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -