'Having a problem in deleting items in mongoos using the findOneByIdAndRemove() function

I'm solving a FREECODECAMP challenge, to pass this challenge I need to remove a person by his ID.

the problem is that I tried the suggested function like findByIdAndRemove() and I still can not pass the challenge and the console shows this err :

(node:9144) [MONGODB DRIVER] Warning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
(Use `node --trace-warnings ...` to show where the warning was created)
{
  _id: new ObjectId("61f2637736f4aea8452a638e"),
  name: 'Jason Bourne',
  age: 36,
  favFood: [],
  __v: 0
}
{
  _id: new ObjectId("61f2637736f4aea8452a638e"),
  name: 'Jason Bourne',
  age: 36,
  favFood: [],
  __v: 0,
  count: 0
}

this is my code so far :

const removeById = function(personID, done) {
  Person.findByIdAndRemove(personID, (err, IDR) => {
    if(err) return console.log(err);
    done(null, IDR);
  });
};

Thank you in advance <3



Solution 1:[1]

FindOneAndRemove is deprecated. Please use deleteOne

const user_id = "user_id_to_delete";
await collection.deleteOne({_id: user_id});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sujan Stc