首页 文章

猫鼬|如何从此mongoose模式中删除重复错误[重复]

提问于
浏览
0

这个问题在这里已有答案:

我在控制台上遇到此错误:

{MongoError:E11000重复密钥错误集合:mean.companies index:students_1 dup key:{:undefined} at C:\ mean \ node_modules \ mongodb-core \ lib \ connection \ pool.js:595:61 at authenticateStragglers(C :\ mean \ node_modules \ mongodb -core \ lib \ connection \ pool.js:513:16)在Connection.messageHandler(C:\ mean \ node_modules \ mongodb-core \ lib \ connection \ pool.js:549:5)在Socket的emitMessageHandler(C:\ mean \ node_modules \ mongodb-core \ lib \ connection \ connection.js:309:10) . (C:\ mean \ node_modules \ mongodb -core \ lib \ connection \ connection.js:452:17)在Socket.emit(events.js:180:13)的addChdChunk(_stream_readable.js:269:12)处的readableAddChunk (_stream_readable.js:256:11)在TCP.onread(net.js:581:20)处的Socket.Readable.push(_stream_readable.js:213:10)名称:'MongoError',消息:'E11000重复键错误collection:mean.companies index:students_1 dup key:{:undefined}',ok:0,errmsg:'E11000 duplicate key error collection:mean.companies index:students_1 dup key:{:undefined}',code:11000,codeName :'DuplicateKey'}

我有2个架构用户和公司,我想用一把钥匙互相连接:这是我的Uschema

const UserSchema = mongoose.Schema({
nama:{
    type    : String
},
email:{
    type    : String,
    require : true,
    unique : true
},
nim:{
    type    : String,
    require : true,
    unique  : true
},
jurusan:{
    type : String
},
prodi:{
    type : String
},
angkatan:{
    type : String
},
password:{
    type    : String,
    require : true
},
company:{
    type : String,
    ref  : 'company',
    unique  : false
}

});

这是CompanySchema:

const CompanySchema = mongoose.Schema({
    perusahaan:{
        type    : String,
        require : true
    },
    deskripsi:{
        type    : String,
        require : true
    },
    alamat:{ 
        type    : String,
        require : true
    },
    email_perusahaan:{
        type    : String,
        require : true
    },
    telepon:{
        type    : String,
        require : true
    },
    website:{
        type    : String,
        require : true
    },
    status:{
        type    : String,
        require : false
    },
    students:[{
        type: String,
        ref: 'user',
        unique  : false
    }]
});

1 回答

  • 0

    从终端打开mongo shell并使用名称为“mean”的数据库

    并写下这个

    db.companies.dropIndex("students_1");
    
    **OR remove all indexes.**
    
    db.companies.dropIndexes();
    

相关问题