【发布时间】:2021-12-12 04:35:10
【问题描述】:
所以基本上它适用于博客文章及其类别和子类别。一篇博文可以有多个类别,每个类别都可以有多个子类别(基于博文)。
例如,假设我有两个帖子,帖子 1 和帖子 2。帖子 1 属于动机和旅程类别。根据帖子 1 的内容,动机可以分为成功和金钱等子类别。
同样,帖子 2 可以属于“教育”和“旅程”类别。教育有一个子类别知识,而旅程有一个子类别称为成功。
我已经设计了 Post 和 Category 架构,但我很难使用 Subcategory 架构。
Post 架构如下所示:
const postSchema = new mongoose.Schema( {
post: {
type: String,
required: true,
trim: true
},
category: [ {
_id: false,
categoryID: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Category'
}
} ]
});
这是类别架构:
const categorySchema = new mongoose.Schema( {
catName: {
type: String,
required: true
},
count: {
type: Number,
default: 0
}
});
*******COUNT field shows the total number of posts that have that specific category.
For example, Motivation category could be in 100 posts.*******
所以我的问题是,如何编写子类别的架构?
如果我将子类别引用放在帖子架构中,那么我将无法将子类别与类别链接,因为每个类别都会根据帖子有不同的子类别。
如果我将子类别引用放在类别架构中,那也是不可行的,因为一个类别可以在多个帖子中。
或者我应该将帖子引用和类别引用放在子类别架构中?
请建议我最好的方法。
【问题讨论】:
标签: mongodb mongoose database-design schema mongoose-schema