同一架构中的引用字段
Reference field from within the same schema
我有一个模式
const mySchema = new Schema({
limit: Number,
currentlyAdded: Number
});
我希望 currentlyAdded
最多 limit
。
max
似乎不接受函数所以我做不到 function() { return this.limit }
有没有办法以某种方式从 currentlyAdded
引用 limit
字段,这样我就可以做类似的事情
const mySchema = new Schema({
limit: Number,
currentlyAdded: {
type: Number,
max: %reference_to_limit%
}
});
您可以使用validate
属性通过调用函数来验证限制条件,
const mySchema = new Schema({
limit: Number,
currentlyAdded: {
type: Number,
validate: [checkLimit, "Exceeded the limit!"]
}
});
// CHECK VALIDATION
function checkLimit(currentlyAdded) {
return currentlyAdded <= this.limit;
}
我有一个模式
const mySchema = new Schema({
limit: Number,
currentlyAdded: Number
});
我希望 currentlyAdded
最多 limit
。
max
似乎不接受函数所以我做不到 function() { return this.limit }
有没有办法以某种方式从 currentlyAdded
引用 limit
字段,这样我就可以做类似的事情
const mySchema = new Schema({
limit: Number,
currentlyAdded: {
type: Number,
max: %reference_to_limit%
}
});
您可以使用validate
属性通过调用函数来验证限制条件,
const mySchema = new Schema({
limit: Number,
currentlyAdded: {
type: Number,
validate: [checkLimit, "Exceeded the limit!"]
}
});
// CHECK VALIDATION
function checkLimit(currentlyAdded) {
return currentlyAdded <= this.limit;
}