I'm creating a website which schools can signup and manage their students' information and data. I want every school to have his own collection of data in the database. The structure of the database which I want to create is like:
DATABASE NAME (database)
— SCHOOL USERNAME (collection)
— — STUDENTS (subcollection)
— — — STUDENT (document)
— — — STUDENT (document)
— — — STUDENT (document)
— ANOTHER SCHOOL USERNAME (collection)
— — STUDENTS (subcollection)
— — — STUDENT (document)
— — — STUDENT (document)
— — — STUDENT (document)
That’s how I want my database to look like. But I’m wondering how to do. Can anyone help me.ns
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 then let the schema design follow the query pattern.
Reading from your description, I think that you can create a data model where the database name is the name of the school and then this database has different collections like- students, teachers, administrative staff, etc and within these collections, you add the details of the corresponding entity:
This would also help in faster reads and writes since one school would want to read and update records related to their students only.
Or if there are just students’ record in your database, then you can model it by having a database and having separate collections for each school and for each collection, having the students’ documents corresponding to that school collection.
Ultimately, as mentioned earlier, it would boil down to the queries that you are intending to use in your application. You can use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.
Regarding creating collections and documents in MongoDB, you can refer to this guide: Create a Collection
Thanks @Satyam,
By the first approach you mentioned, the database name isn’t same as the school name. There is only one database. It has collections where collection name is same as the school username. Every collection/school has subcollections like students, teachers, classes, etc. How can I do that with mongoDB?
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:
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.