我目前正在为自己正在构建的应用计划数据库结构,this linked answer中的某些内容对我提出了一些问题。
在Shivam提出的结构中,他有时直接引用另一个集合,换句话说,在一个模式中,他将字段的类型定义为另一种模式类型的数组。示例:
import { Schema } from "mongoose";
import { QuestionSchema } from "./question-schema";
const mongoose = require('mongoose');
export const QuestionSetSchema: Schema = new Schema({
questionSet: {
type: [QuestionSchema],
validate: {
validator: function(value: any) {
return value.length === 12;
},
message: 'Question set must be 12.'
}
},
}, {
timestamps: true
});
在其他情况下,他仅使用对另一个模式/集合的ObjectID引用:
export const CandidateSchema: Schema = new Schema({
name: String,
email: String, // you can store other candidate related information here.
totalAttempt: {
type: Number,
default: 0,
validate: {
validator: function(value: number) {
return value === 3;
},
message: 'You have already done three attempts.'
}
},
candidateQuestionAnswers: {
type: [Schema.Types.ObjectId],
ref: 'CandidateQuesAnswer'
}
}, {
timestamps: true
});
以上每种情况的用例是什么? “ type:[otherSchema]”方法实际上是嵌入该集合的实例,还是仅向调用它们的Schema提供其属性?