Node.js Mongoose验证程序未更新

Node.js Mongoose验证程序未更新,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在使用mongoose和npm包mongoose唯一验证器进行验证 当插入我的模式时,它工作得很好,但我将验证从一个属性更改为另一个属性。现在,我仍然得到唯一的mongo错误的旧验证(MongoError11000)的电子邮件。。。虽然我把它移到了用户名,好像以前的验证器没有被删除,也没有正确更新 const mongoose = require('mongoose'); const uniqueValidator = require('mongoose-unique-validator')

我正在使用mongoose和npm包
mongoose唯一验证器
进行验证

当插入我的模式时,它工作得很好,但我将验证从一个属性更改为另一个属性。现在,我仍然得到唯一的mongo错误的旧验证(MongoError11000)的电子邮件。。。虽然我把它移到了用户名,好像以前的验证器没有被删除,也没有正确更新

const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const Schema = mongoose.Schema;

let userSchema = new Schema({
    username:{
        type: String,
        required: true,
        unique: true,
        uniqueCaseInsensitive: true
    },
    email: {
        type: String,
        required: true
    }
}
userSchema.plugin(uniqueValidator, {message : 'username must be unique'});

module.exports.User = mongoose.model('User', userSchema);

有没有办法更新验证器?我还没有找到任何东西

请完全删除您的收藏,然后重新运行您的项目。在那之后一切都会好起来的,因为在你的mongodb中基于你的电子邮件创建的唯一索引仍然存在

对于删除索引,您可以使用mongodb的IDE,如Robo3T或。。。 使用命令可以删除索引

db.getCollection('CollocationName').dropIndex( "indexName_1" )
给你的是:

db.getCollection('users').dropIndex( "email_1" )
如果索引是由mongoose创建的,则应使用带有_1的indexName