簡體   English   中英

同一架構中的引用字段

[英]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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM