Answer the question
In order to leave comments, you need to log in
Is it possible to store references to documents of another collection in MongoDB and is this the best solution in this situation?
Good day to all
Now I am developing a non-trivial CRM system and there are some difficulties in the correct design of the database.
Let's start with a little introduction.
Database - MongoDB
Server - NodeJS
ODM - Mongoose + Typegoose (typescript project)
CRM system is developed for online school.
So, we have the database itself, and it is divided into two collections: Pupils and Groups
The Pupils collection stores documents of this format:
And the following documents are stored in the Groups collection:
Total: there are 2 entities - students and groups in which they will be to study.
As you may have noticed, the group object contains the PUPILS field, which stores the students studying in this group. But not specifically the student objects themselves, but their IDs in the Pupils collection.
I refused to duplicate these objects from the collection of students into groups, because I was told that duplicate data in two different collections is bad.
That is, we get the following object in the database:
But when the frontend renders the page, it will eventually need to first get the group object via the API, then make another request to get student objects from the array.
Maybe it doesn’t sound terrible, but the schedule needs to be kept separately for each student, and there are a lot of other nuances that I won’t even list so as not to be distracted from the main two problems.
Problem 1:
Is it possible for these identifiers to be object references from the Pupils collection? So that they are stored in the database as:
but when I make a request to get groups / a specific group, the database would return an object of this kind to me:
Where
["id1", "id2", "id3"]
{
...
}
and there is Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question