E
E
Evgeny Zhurov2021-06-20 00:15:08
Node.js
Evgeny Zhurov, 2021-06-20 00:15:08

How to set up interaction between posts and categories (Mongodb, Node.js)?

The project has entities that can be conditionally called posts, and there are entities that can be called categories. For example, a post is a music album. And there are categories - 'artist', 'label' and 'genre'. An album can be assigned to multiple artists, multiple labels, and multiple genres. For example, when creating a post in album.Schema, I use a field like this:

artists: [
    {
       type: Schema.Types.ObjectId,
       ref: 'artists',
       required: true
    },
],


Similarly with the labels and genres fields. In fact, the id of these categories are stored in the database.
And then - I don’t really understand how I can pull out the names and id of the specified categories in the output? For example, I pass a list of albums to the front, and in addition to the actual album fields, I need to transfer not only id, but also the names of categories (artists, labels and genres).

Also, categories have their own pages, and they need to pass a list of albums that are associated with this category. That is, it turns out that when saving a new post, you need to somehow update the category entries as well? For example, I created a Master of Puppets post and assigned it the (pre-created) entry 'Metallica' as the artist category. And I want both the master of Puppets post to be linked to the Metallica record and the metallica record to be linked to the Master of Puppets post.

Moreover)) - In the same post, I also assign some label(s) and genre(s). Accordingly, it is necessary that:
- When displaying all albums (and on the page of the album itself), I get the names and id of labels, genres and artists
- On the artist page, I get all his albums, genres and labels
- On the label page - all his albums, genres and artists
- On the genre page - all albums, labels and artists

If I understand correctly, this is called many-to-many? The question, in principle, is what kind of operations they are, how best to do them so as not to overload the server, and where to dig at all? Are there any good articles/vidos where you can learn how to do this? Well, or if (suddenly) this is a simple ordinary operation, then I would be grateful if you write in the answer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xirurgx3d, 2021-06-20
@xirurgx3d

try to use

populate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question