The usage scenario is like this:
When publishing an article, it is accompanied by tags. There are many tags in an article,
Then when querying a tag, how to query all the articles of this tag? ?
Then what is the entire mongoodb data design idea? Please give me some answers, thx
The article contains _id, title, tags, content
Then, tags contains many tags
_id
title
tags
tag1
tag2
tag3
content
You can design it like this
You can write a method to return all articles under a specific tag
Then call it in the corresponding route
When saving the article, add the corresponding tag id to the tags array (foreign key), and at the same time add the article id (foreign key) to all corresponding tags
This is a typical many-to-many model, the table design is as follows
Article table article
tag tabletags
Article and tag association table article_tag
Specific ideas
Associate the article table and tags table through the foreign key constraint of article_tag
For article tag operations, if the tag already exists, you only need to add or delete the article_tag table data
If the tag does not exist, add the tag and article first, and then add
Article ID<->Tag ID
data to article_tagIf you want to query all articles in a tag, use the article_tag table to left-join (or inline) the article table
If you want to query all the tags of an article, use the article_tag table to left-join (or inline) the tags table
In the above two cases, if you still need tag or article data, just continue to left-link the tag table or article table.
I agree with the solution above. One table for articles, one table for tags, and then create a corresponding table of article tags