Why is the select:false property on mongoose schema not working?

Viewed 52

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
  });
0 Answers
Related