let itemSchema = new Schema({
description: String,
evaluations: [
{
evaluation: {
type: Schema.Types.ObjectId,
ref: 'evaluation',
required: true,
index: true
},
selection: {
type: Schema.Types.ObjectId,
ref: '????', // How do i reference to 'selection' field here?
required: true,
index: true
}
}
]
let evaluationSchema = new Schema({
name: {type: String, required: true},
selections: [
{
name: {type: String, required: true },
value: { type: Number, min: 0, max: 15 },
}
],
});
是否可以对selections数组中的项目进行引用?该语法如何编写?在文档中找不到。
答案 0 :(得分:0)
请先为两个模式都指定模型的集合名称,然后在引用中输入EvaluationSchema的集合名称,
另一个您不能引用字段,您只能引用模式(引用字段可以使用$ project在mongodb聚合中完成)
let itemSchema = mongoose.Schema({
description: String,
evaluations: [
{
evaluation: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Eval',
required: true,
index: true
},
selection: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Eval', // How do i reference to 'selection' field here?
required: true,
index: true
}
}
]
});
mongoose.model("Item", itemSchema);
let evaluationSchema = mongoose.Schema({
name: {type: String, required: true},
selections: [
{
name: {type: String, required: true },
value: { type: Number, min: 0, max: 15 },
}
],
});
mongoose.model("Eval", evaluationSchema);