我在模型上查詢interventions使用 MongoosefindById方法呼叫的 Mongo 集合Intervention。該查詢回傳關聯模型中定義的所有欄位。
集合中student也存在一個欄位interventions。貓鼬沒有歸還它。據推測,這是因為student在干預模型中沒有定義復雜物件(25-30 個鍵/值對)。我永遠不需要將檔案插入到干預模型中。
import mongoose from 'mongoose'
const { Schema } = mongoose
const interventionSchema = new Schema(
{
abs_count_excused: { type: Number },
abs_count_unexcused: { type: Number },
abs_count_total: { type: Number },
student_id: { type: Number, required: true }
}
)
const Intervention = mongoose.model(
'Intervention',
interventionSchema,
'interventions'
)
export default Intervention
是否可以在student不在干預模型中定義的情況下檢索子檔案?
謝謝你。
uj5u.com熱心網友回復:
我不相信你可以從 MongoDB 中檢索一個值而不以某種方式將它作為你的 Mongoose 模式的一部分。這似乎是Mongoose的混合模式型別的機會。
因為您關心的是將復雜student檔案轉換為模式,并且您不打算通過Intervention模型更新它,所以Mixed模式型別的缺點不適用。因此,您可以將student欄位添加到您的架構中,如下所示:
const interventionSchema = new Schema(
{
abs_count_excused: { type: Number },
abs_count_unexcused: { type: Number },
abs_count_total: { type: Number },
student_id: { type: Number, required: true },
student: { type: Object }
}
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/389736.html
