I want to ask a question about mongodb (mongoose).
A blog has two tables, articles and tags, a many-to-many relationship.
The tag field in the article table is an array, which stores the tags. id, when querying articles, the data of each tag can be associated.
The current demand is:
Query tag When making a list, you need to get at the same time how many articles each tag is included in, that is, count.
In addition to traversing and manually establishing the count field and re-counting when adding, deleting, or modifying the count field
Is there a better way to achieve it through the mongoose API? Method, thank you all!
Solved, aggregation query is required. Decompose before aggregation, and then aggregate. Specific code. In fact, aggregation query itself can query requirements, but the requirements are many-to-many data relationships, not one-to-many, so they must be decomposed into one-to-one first. Relationship
Related code snippets
Related reference documents
There is a very useful tool called Population in mongoose. Please check it out