【问题标题】:Mongoose | How to remove duplicate error from this mongoose schema [duplicate]猫鼬 |如何从此猫鼬模式中删除重复错误[重复]
【发布时间】:2018-10-09 06:37:23
【问题描述】:

我的控制台出现了这个错误:

{ MongoError: E11000 重复键错误集合:mean.companies 索引:students_1 复制键:{:未定义} 在 C:\mean\node_modules\mongodb-core\lib\connection\pool.js:595:61 在 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) 在 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) 在 addChunk (_stream_readable.js:269:12) 在 readableAddChunk (_stream_readable.js:256:11) 在 Socket.Readable.push (_stream_readable.js:213:10) 在 TCP.onread (net.js:581:20) 名称:'MongoError',消息:'E11000 重复键错误集合:mean.companies 索引: student_1 dup 键:{:未定义}',确定:0,errmsg:'E11000 重复键错误集合:mean.companies 索引:students_1 dup 键:{:未定义}',代码:11000,代号:'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
    }]
});

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

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

    然后写这个

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

    【讨论】:

      猜你喜欢
      • 2017-11-20
      • 2019-02-22
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 2017-08-01
      • 2015-09-09
      • 1970-01-01
      • 2013-11-07
      相关资源
      最近更新 更多