去除mongoose操作mongodb数据库文档中多出的下划线下划线v即__v字段

在创建集合的时候,默认会多出__v字段来表示版本,如下图所示

如果想在数据库中不显示__v字段,可以在定义schema规则的时候通过设置versionKey:false去除这个字段:

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

 如果在数据库中扔向保留这个字段,只是在查询的时候不想返回__v字段,可以通过设置{ __v: 0}在返回结果中过滤掉这一字段

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

猜你喜欢

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