I have a database in MongoDB that contains an array with company names. I need to remove a specific element from the array based on its position. So, I crafted a query to achieve this.
{
company: [
{name: "exist"},
{name: "cool"},
{name: "ho"}
]
}
The initial query works perfectly, but now I want to dynamically specify the position to remove an element. Unfortunately, the code I tried gives me an error.
await Company.findOneAndUpdate({
_id: "xdef"
},
{
$unset: {
`company.{req.query.position}`: 1
}
}
})
If you have any suggestions on how to handle this issue, I would greatly appreciate it. Thank you for taking the time to read this.