I am trying to push to a document then set isDefault to false for all except for the one I want to set. I am having issues setting it as I am getting MongoError: Updating the path 'cards' would create a conflict at 'cards'
this is my query
const update = {
$push: { cards: card },
$set : { 'cards.$[].isDefault': false },
};
const query = { id: customer.id }
this.model.findOneAndUpdate(
query,
update,
{ new: true },
(err, result) => {
if (err) {
return reject(err);
}
if (!result) {
return reject(
new ModelNotFoundError(
`${this.name} not found update with operators`
)
);
}
resolve(result);
}
);
This is what my data structure looks like
[
{
id: 1,
cards: [
{
name: "Shipit",
fee: 4,
isDefault: false
},
{
name: "Shipit",
fee: 3,
isDefault: true
}
],
},
{
id: 2,
cards: [
{
name: "Shipit",
fee: 3,
isDefault: true
}
],
}
]