Data structure
"classrooms" : [
{
id : ObjectId("class1")
"students" : [
ObjectId("student1")
],
},
{
id : ObjectId("class2")
"students" : [
]
}
],
"students" : [
{
"id" : ObjectId("student1"),
"firstname" : "Ciljan",
"age" : NumberInt(23)
},
{
"id" : ObjectId("student2"),
"firstname" : "John",
"age" : NumberInt(19)
}
]
The need
I have to provide a document where there are classrooms and students. Students could be assigned to a specific classroom, but not always, so a student could keep free. With the previous data structure, I have the subdocument "students" that provides the list of all the available students both assigned and not assigned. I can assign a student to a classroom storing the student id in the classrooms students array.
Problem
How can I retrieve these data in order to have for each classroom all the records about their assigned students?