Home > Software engineering >  No functionality in mongoose Model.deleteOne()
No functionality in mongoose Model.deleteOne()

Time:12-20

I am trying to use the mongoose Model.deleteOne() method to delete an object within a collection. However, Model.deleteOne() provides no result at all.

Code:

const id = interaction.options.get('id')?.value as string

ModerationData.deleteOne({
    _id: id,
    guild_id: interaction.guild?.id
})

CodePudding user response:

I see you are using _id to find the required document and then trying to delete it. You can use findByIdAndDelete() method as well for the same functionality. Here is a code snippet for the same -

ModerationData.findByIdAndDelete(id, function (err, docs) {
  if (err) {
    console.error(err)
  } else {
    res.redirect("/home");
  }
});

Here id is a variable holding the ObjectID value for the document.

CodePudding user response:

Try findOneAndRemove

const id = interaction.options.get('id')?.value as string

ModerationData.findOneAndRemove({
    _id: id,
    guild_id: interaction.guild?.id
})
  • Related