[英]mongoose populate nested schema without model
我正在嘗試填充嵌套模式而不為子模式創建模型,但沒有任何成功。
我有一個'問題'模型,由2個模式創建(問題,選項)
const Option = new mongoose.Schema({
value: { type: String, required: true }
})
const Question = new mongoose.Schema({
content: { type: String, required: true },
options: [Option]
})
module.exports = mongoose.model('Question', Question)
我有一個'評論'模型
const Review = new mongoose.Schema({
results: [
{
question: { type: mongoose.Schema.Types.ObjectId, ref: 'Question' },
option: { type: mongoose.Schema.Types.ObjectId, ref: 'Question.options' }
}
],
critical: { type: Boolean, default: false }
})
module.exports = mongoose.model('Review', Review)
好吧,我想創建響應數組審核文檔的API /評論,但填充問題和選項。
我嘗試這個命令,但它不起作用。
Model.find({}).populate('results.option')
任何的想法?
因為populate不能引用子文檔數組,因為你有單獨的Option模式,為什么不使用它。
const Review = new mongoose.Schema({
results: [
{
question: { type: mongoose.Schema.Types.ObjectId, ref: 'Question' },
option: { type: mongoose.Schema.Types.ObjectId, ref: 'Option' }
}
],
critical: { type: Boolean, default: false }
})
module.exports = mongoose.model('Review', Review)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.