如果我的模式中有一个属性依赖于另一个属性(比如它的最小值),如何在我的模式中定义它?
我的模式中有一个 endDate 和一个 actualEndDate 属性,第二个总是大于或等于第一个,我如何将它放入我的模式中
const schema = new mongoose.Schema({
endDate: {
type: Date,
min: new Date(),
required: true
},
actualEndDate: {
type: Date,
min: new Date(), // I need this to be min: this.endDate or something
}
});
最佳答案
您可以添加自己的自定义验证。
试试这个:
endDate: {
type: Date,
required: true,
// min: new Date()
// default: Date.now
},
actualEndDate: {
type: Date,
validate: [
function (value) {
return this.endDate <= value;
}
]
},
关于node.js - 如何在 Mongoose 模式中引用另一个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57754295/