您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Mongoose模式中设置数组大小的限制

如何在Mongoose模式中设置数组大小的限制

稍微调整一下架构设置,即可添加验证选项:

var peopleSchema = new Schema({
  name: {
    type: String,
    required: true,
    default: true
  },
  friends: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'peopleModel'
    }],
    validate: [arrayLimit, '{PATH} exceeds the limit of 10']
  }
});

function arrayLimit(val) {
  return val.length <= 10;
}
mongodb 2022/1/1 18:14:30 有457人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶