I am trying to update an embedded array document in a single function but I am getting an error:
Updating the path ‘x’ would create a conflict at ‘x’
My schema is
const AddressSchema = new Schema({
city: {
type: String,
required: true,
},
isDefault: {
type: Boolean,
required: true,
},
});
const UserSchema = new Schema(
{
username: {
type: String,
},
addresses: [Address.schema],
},
);
My goal is to add a new default address while updating the existing default address to false using this:
User.findOneAndUpdate(
{ username: req.user.username },
{
$set: { "addresses.$[].isDefault": false },
$push: { addresses: { city: "Example City", isDefault: true } },
},
{ new: true }
)
What’s the other way around here. Thank you!