MongoDB 类别和子类别的设计模型
MongoDB design model for category and subcategory
大家好,我遇到了 MongoDB 的问题,我不知道如何设计数据库以支持类别和子类别...
所以我正在尝试创建一个有 2 个模型的测验应用程序:
问题模型
- 我会包含问题
类别模型
- 它包含问题所在的类别
这就是我想要完成的,我想构建测验应用程序,测验应用程序将有 3 个主要类别:A 类、B 类和 C 类...
Note: I will not have more than 20 subcategories per category
每个类别都有自己的子类别,如下所示:'A category' 将包含 Test1、Test2、Test3、Test4、Test5...与 'B category' 相同,它将包含 Test1、Test2 , 测试 3, 测试 4....
Note: I will not have more than 3 main categories
并且每个 Test1、Test2 子类别都将包含一个问题列表
所以我的模型是这样的:
类别模型:
export const CategorySchema = new mongoose.Schema({
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category"
},
name: { type:String, required:true },
slug: String,
});
问题模型:
export const QuestionSchema = new mongoose.Schema({
question: { type:String, required:true },
options: [String],
answers: Array,
category: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category"
}
});
还有一个问题是,我希望问题成为不同子类别的一部分,就像它可以成为
A 类别 > 测试 1,B 类别 > 测试 2,A 类别 > 测试 3, 所以我可以在不同的子类别之间重复使用它
我的问题是:这是设计数据库的正确方法吗?或者有其他方法吗
我正在使用 Category 和 Test 一起形成一个 tests
集合的参考,第二个集合是questions
。我能想到的有两种模型,选择取决于您的查询和应用需求。
模型 1:
tests:
id: "A-Test1"
category: "A",
test: "Test1",
name / description: "...",
questions: [ "q1", "q2", "q15", "q16", "q25", ... ]
questions:
id: "q1",
details: { question: "...", options: [...], answer: [...] }
您想查找类别 "A" 和 "Test2" 的所有问题:
- 查询测试集并获取问题。
- 从问题集合中获取问题详情;这将是一个
"join"查询,使用聚合阶段
$lookup
.
您想知道一个问题是否属于categories/tests:
- 查询测试集合的问题数组。
- 查找问题集以获取有关该问题的更多详细信息。
模型 2:
tests:
id: "A-Test1"
category: "A",
test: "Test1",
name / description: "..."
questions:
id: "q1",
details: { question: "...", options: [...], answer: [...] },
tests: [ "A-Test1", "A-Test5", "C-Test2", ... ]
您想查找 "A" 和 "Test2" 的所有问题:
- 在测试数组上查询问题集合。
- 查找测试集合以获取有关测试的更多详细信息。
你想知道一道题属于什么categories/tests:
- 在测试数组上查询问题集合。
- 查找测试集合以获取有关每个测试的更多详细信息。
大家好,我遇到了 MongoDB 的问题,我不知道如何设计数据库以支持类别和子类别...
所以我正在尝试创建一个有 2 个模型的测验应用程序:
问题模型
- 我会包含问题
类别模型
- 它包含问题所在的类别
这就是我想要完成的,我想构建测验应用程序,测验应用程序将有 3 个主要类别:A 类、B 类和 C 类...
Note: I will not have more than 20 subcategories per category
每个类别都有自己的子类别,如下所示:'A category' 将包含 Test1、Test2、Test3、Test4、Test5...与 'B category' 相同,它将包含 Test1、Test2 , 测试 3, 测试 4....
Note: I will not have more than 3 main categories
并且每个 Test1、Test2 子类别都将包含一个问题列表
所以我的模型是这样的:
类别模型:
export const CategorySchema = new mongoose.Schema({
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category"
},
name: { type:String, required:true },
slug: String,
});
问题模型:
export const QuestionSchema = new mongoose.Schema({
question: { type:String, required:true },
options: [String],
answers: Array,
category: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category"
}
});
还有一个问题是,我希望问题成为不同子类别的一部分,就像它可以成为 A 类别 > 测试 1,B 类别 > 测试 2,A 类别 > 测试 3, 所以我可以在不同的子类别之间重复使用它
我的问题是:这是设计数据库的正确方法吗?或者有其他方法吗
我正在使用 Category 和 Test 一起形成一个 tests
集合的参考,第二个集合是questions
。我能想到的有两种模型,选择取决于您的查询和应用需求。
模型 1:
tests:
id: "A-Test1"
category: "A",
test: "Test1",
name / description: "...",
questions: [ "q1", "q2", "q15", "q16", "q25", ... ]
questions:
id: "q1",
details: { question: "...", options: [...], answer: [...] }
您想查找类别 "A" 和 "Test2" 的所有问题:
- 查询测试集并获取问题。
- 从问题集合中获取问题详情;这将是一个
"join"查询,使用聚合阶段
$lookup
.
您想知道一个问题是否属于categories/tests:
- 查询测试集合的问题数组。
- 查找问题集以获取有关该问题的更多详细信息。
模型 2:
tests:
id: "A-Test1"
category: "A",
test: "Test1",
name / description: "..."
questions:
id: "q1",
details: { question: "...", options: [...], answer: [...] },
tests: [ "A-Test1", "A-Test5", "C-Test2", ... ]
您想查找 "A" 和 "Test2" 的所有问题:
- 在测试数组上查询问题集合。
- 查找测试集合以获取有关测试的更多详细信息。
你想知道一道题属于什么categories/tests:
- 在测试数组上查询问题集合。
- 查找测试集合以获取有关每个测试的更多详细信息。