【发布时间】:2019-01-12 17:32:56
【问题描述】:
我有两个这样的模型
const testRunSchema = new mongoose.Schema({
testTimeSlot: {
type: String,
required: true
},
testDate: {
type: String,
required: true
},
diagnosticData: [Object],
notes: String,
active: {
type: Boolean,
default: true
}
}, {
timestamps: true,
strict: false
})
const testingSchema = new mongoose.Schema({
testId: {
type: mongoose.Schema.ObjectId,
required: true
},
testDetails: {
//dummy data
},
contactDetails: {
//dummy data
},
testRunDetails: [testRunSchema], //is this a best way?
runByAssistant: Boolean
}, {
timestamps: true
});
module.exports = mongoose.model('Testing', testingSchema)
现在我想使用第二个模型的testId 访问testTimeSlot(这是第一个模型)。
我的解决方案:
我可以使用testId访问第一个模型的testimeSlot,因为第一个模型的数据在seconf模型的testRunDetails中可用。
这个解决方案的问题:
由于testRunSchema在第二个模型中被定义为一个数组,因此访问每个数组元素的testTimeSlot并不容易和高效。
解决此问题的最佳方法是什么?
【问题讨论】:
标签: node.js mongodb mongoose mongoose-schema mongoose-populate