how to make a field unique in the subdocument in mongoose code example
Example 1: mongoose unique field
var SimSchema = new Schema({
msisdn : { type : String , unique : true, required : true, dropDups: true },
imsi : { type : String , unique : true, required : true, dropDups: true },
status : { type : Boolean, default: true},
signal : { type : Number },
probe_name : { type: String , required : true }
});
Example 2: mongoose unique subdocument
Model.update({
_id: '576328595b2880f831413b92',
'subdocs.name': {
$ne: 'Unique Name'
}
}, {
$push: {
subdocs: {
name: 'Unique Name'
}
}
}).then((raw) => {
}).catch(next);