Your problem is findByIdAndRemove()
looks for the _id
and you want to use other value to execute the query. So you can use findOneAndDelete()
function from Mongoose
.
There are also more functios to do the job as deleteOne
or findAndDelete
. But if you want to delete by id I recommend use findOneAndDelete
because return the deleted document.
Then you have to pass the id
to query like this:
var deleted = await model.findAndDelete({id: "myID"})
Note that id
is not the same as _id
.
And that’s all…
From documentation, to know how functions works:
Deletes a single document based on the filter and sort criteria, returning the deleted document.
Removes a single document from a collection.
And also, if you are using mongoose
and you want delete by _id
you can use findByIdAndDelete()
function instead of findByIdAndRemove()
.
According to documentation
This function differs slightly from Model.findOneAndRemove() in that findOneAndRemove() becomes a MongoDB findAndModify() command, as opposed to a findOneAndDelete() command. For most mongoose use cases, this distinction is purely pedantic. You should use findOneAndDelete() unless you have a good reason not to.
CLICK HERE to find out more related problems solutions.