Need to a save new record even if one record already exists with status false in unique schema using mongoose node js. I have one record along with {status:false}
by updating status true to false while deleting that record. Now i try to add a new record. but new record is not crated. because i have apply unique: true
in mongoose schema. I need to apply unique : true, but same time need to create new record if it's status false for exist one. So can anyone give some suggestion
Mongoose, nodejs with mongoose-beautiful-unique-validator
//schema model
var userSchema = new mongoose.Schema({
userName: {
type: String,
required: true,
unique: true
})
//save user function inside module export function
const user= mongoose.model('userSchema');
user.userName = fields.username;
user.save((err, doc) => {
if (!err){
res.send(true);
} else {
console.log(err);
return;
}
})
Expected result:
[
{_id: 12345, userName: 'Uday', status:false},
{_id: 67894, userName: 'Uday', status:true}
]
Actual result: I got duplicate error When i try to save name with Uday with status: true
[
{_id: 12345, userName: 'Uday', status:false},
]