mongodb数据库mongoose查询时排除过滤某个字段的两种方法

以过滤掉password字段为例

1. password:0

User.findOne({username, password: md5(password)}, {password:0}, function (err, user)
{
 //处理逻辑
})

2. select:false

直接在定义schema规则的时候就规定查询的时候不返回password

var userSchema = new mongoose.Schema({
    username: {
        type:String,
        required:true,
        minlength:2,
        maxlength:20
      },
    password: {
            type:String,
            required:true,
            select:false
          },
   
    })

猜你喜欢

转载自blog.csdn.net/a1059526327/article/details/106965195