为什么选择:Mongoose模式上的False属性不起作用?

发布于 2025-01-31 13:30:36 字数 860 浏览 6 评论 0原文

我对猫鼬模型有以下架构:

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'User must have a name'],
    unique: true
  },    
  photo: String,
  password: {
    type: String,
    required: [true, 'User must have a passwod'],
    select: false,
    minlength: 8
  },
  passwordConfirm: {
    type: String,
    required: ['Please confirm the passwod'],
    select: false,
    validate: {
      validator: function(val) {
        return this.password === val;
      },
      message: 'Password and passwordConfirm do not match'
    }
  }
});

由于密码设置为SELECT:false,不应在查询对象中存在。但是,当我在下面创建文档时,它始终包含密码:

const user = await userModel.create({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password,
    passwordConfirm: req.body.passwordConfirm
  });

I have the following schema for a mongoose model:

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: [true, 'User must have a name'],
    unique: true
  },    
  photo: String,
  password: {
    type: String,
    required: [true, 'User must have a passwod'],
    select: false,
    minlength: 8
  },
  passwordConfirm: {
    type: String,
    required: ['Please confirm the passwod'],
    select: false,
    validate: {
      validator: function(val) {
        return this.password === val;
      },
      message: 'Password and passwordConfirm do not match'
    }
  }
});

Since password is set as Select:false, it should not be present in the queried object. However, when i create a document as below, it always has the password present:

const user = await userModel.create({
    name: req.body.name,
    email: req.body.email,
    password: req.body.password,
    passwordConfirm: req.body.passwordConfirm
  });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文