I got MongoError 11000 cause key duplicate or it should be unique as imposed in the schema. below,my schema.
var schema = new mongoose.Schema({
make: {type: String, trim: true, required: true},
model: {type: String, trim: true, required: true},
year: {type: Number},
enabled: {type: Boolean, default: false},
range: {type: String, enum: _.values(Range)},
premium: {type: Boolean, default: false},
picture: {},
status: {type: String, enum: _.values(CarModelStatus), default: CarModelStatus.DRAFT}
});
schema.index({model: 1, make: 1, year: 1}, {unique: true});
and this is my code. (I tried to catch the error and return
const toFix = {
'Mercedes': 'Mercedes-Benz'
};
console.log('Clean carModels...');
async.forEachOfSeries(toFix, function (to, from, cb1) {
console.log(`FixMakeForCars ${from} -> ${to}`);
CarModel.update({'make': {$regex: S(from).trim().s, $options: 'i'}},
{$set: {'make': to}}, {multi: true}, function (err, res) {
if (err) {
if(err.name === 'MongoError' && err.code === 11000){
return console.log('carModel already exist');
}
return err.code
}
cb1()
});
}, cb);
but even this way it doesn't solve it.
any suggestions please?
via Amine Haddar
No comments:
Post a Comment