DeprecationWarning (model.remove)

Section: Delete Many Documents with model.remove()

I can’t seem to get over the “deprecation warning” issue that a lot of people have also encountered. After running the test, “Mary” is added to the database but isn’t deleted. I have already tried the following:

  • Made sure mongodb was 3.6.0 & mongoose was 5.4.0
  • Used the answer code for “removeManyPeople”
  • Used “deleteMany” instead
  • Tested both locally & on Replit

Code used:

const removeManyPeople = (done) => {
  const nameToRemove = 'Mary';
  Person.remove({ name: nameToRemove }, (err, response) => {
    if (err) return console.log(err);
    done(null, response);
  });
};

Package.json:

"dependencies": {
        "body-parser": "^1.15.2",
        "dotenv": "^8.2.0",
        "express": "^4.12.4",
        "mongodb": "~3.6.0",
        "mongoose": "~5.4.0"
    }

Console Message:

DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.

Any advice is appreciated.

So I managed to solve my issue. After looking at the tests in the server file, I found that it checked for properties “ok” and “n”. Since the response data only had properties “acknowledged” and “deletedCount”, I just manually set them myself and the test passed.

I also got a " SyntaxError: Unexpected token o in JSON at position 1" error so I had to JSON.stringify() the response data.

Also I am using the latest versions of mongodb and mongoose so no need to downgrade.

const removeManyPeople = (done) => {
  const nameToRemove = 'Mary';
  Person.deleteMany({ name: nameToRemove }, (err, data) => {
    if (err) return console.log(err);
    data.ok = true;
    data.n = data.deletedCount;
    done(null, JSON.stringify(data));
  });
};