R
R
Rag'n' Code Man2021-06-03 22:12:44
MongoDB
Rag'n' Code Man, 2021-06-03 22:12:44

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
60b921c879610786191373.jpeg

The Pupils collection stores documents of this format:
60b922eae95dd650689909.png

And the following documents are stored in the Groups collection:
60b928faed3b4837271591.jpeg

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:
60b92552a9b0d415912995.jpeg

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"]
60b927679dd42718507899.jpeg

{
...
}
and there is
60b9277fef959818303081.png

Problem 2
If there is such a possibility, is it possible to supplement this link with your own data? To end up with the following data in the collection of groups:
60b928af8cfda706020507.jpeg
So that in the end this Reference is expanded into a student object and these fields are added to it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Romi, 2021-06-03
@romicohen

The opinion of an amateur (in Monge): for such projects it is better not to take a noSQL database. Take regular MySQL or something like that. There all such questions are rhetorical :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question