Hey @Mohamed_Abdillahi,
Thanks for the reply.
The approach you described seems a bit tricky to me. One suggestion I would give is to name your collections starting with the name of the school and then the entity that you want to include in that collection, ie.:
school1.students //collection 1
school1.teachers //collection 2
school1.classes. // collection 3
school2.students // collection 4
school2.teachers //collection 5
.... so on
With this approach, you can use lookup operator when needing to search across many collections of the same school.
You can also do this by creating a students and teachers etc collection, then in the documents, specify the school name, ie. keep a field named School in the documents itself. This way, your student document might look like this:
{
"_id": ObjectId("61501c6d24a680f9947d36f9"),
"name": "John Doe",
"age": 18,
"grade": 12,
"school": "XYZ High School"
},
{
"_id": ObjectId("61501c6d24a680f9947d36fb"),
"name": "Paul Doe",
"age": 17,
"grade": 11,
"school": "ABC High School"
}
and similarly, you can store a school field in your other collections too like teachers, staff, etc.
I still didn’t get why keeping different schools as different databases can’t be done. Is there any particular reason why we are dismissing this thought? With this approach, it would look like this:
School_1, School_2, etc are the databases’ names. And within them are your collections of students, teachers, etc. You can switch between different databases using the
use <db_name> command. If you have any doubts about how to create or use multiple databases or collections, you can go through the following documentation: Databases and Collection
As you can see, there are many approaches that you can use while designing your model. I would suggest you explore all the alternatives before deciding there has to be only one database or limit your other options.
A general rule of thumb while modeling data in MongoDB is that things that are queried together should stay together. Thus, it may be beneficial to work from the required queries first, making it as simple as possible, and let the schema design follow the query pattern. You can use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.
I’m also attaching some more resources that you should find useful:
MongoDB Data Modelling Course
MongoDB Documents
FAQ: MongoDB Fundamentals
Hope this helps.
Regards,
Satyam
