MongoDB and Mongoose - Delete Many Documents with model.remove()

code not working. I am working in local system

package.json

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

myApp.js

const personSchema = new mongoose.Schema({
   name: {
      type: String
   },
   age: {
     type: Number
   },
   favoriteFoods: [{
     type: String
   }]
});

const removeManyPeople = (done) => {
   const nameToRemove = "Mary";

   Person.remove({name: nameToRemove}, (err, data) => {
      if(err){done(err)}
      done(null, data);
      })
};

Challenge: MongoDB and Mongoose - Delete Many Documents with model.remove()

Link to the challenge:

You appear to have installed a different version of mongoose than was specified in the first challenge of this series. You should not have change the version of mongoose as the correct version of mongoose was already in the package.json file. I suggest reviewing the first lesson in this series, unistalling mongoose and then reinstalling the correct version as specified in the first challenge.

Also, it is always better to put a link to your project code in the topic instead of just putting a few bits of code from the project. This allows us to test your project as it is. If you are doing the project locally, then you should push your project up to GitHub in a repo, so we can clone it locally ourselves and test it.

Lastly, can you give us more information about what is “not working”? Do you see any errors in the Node console when running the app? Do you see any errors in the browser console when you try to submit a link to your live project? What url are you using to submit for the app?

I have cloned the given repo again. I have been running it on local system but it’s still not working.

GitHub - https://github.com/antu99g/freeCodeCamp-mongoDB

local console :

Your app is listening on port 3000
OPTIONS
POST
(node:10144) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[object Object]

test error on freeCodeCamp :

[Error: E11000 duplicate key error collection: freeCodeCamp_task.people index: name_1 dup key: { name: "Mary" }]

You likely had it set as unique in the Schema at one point. Delete the collection on Atlas and try the submit again (still without using unique in the Schema).

No, I have not set any value for unique in my Schema.
This is my Schema :

const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);

const personSchema = new mongoose.Schema({
  name: {
    type: String
  },
  age: {
    type: Number
  },
  favoriteFoods: [{
    type: String
  }]
});

let Person = mongoose.model('Person', personSchema);

It doesn’t matter how the Schema looks now. Are you sure you at some point didn’t use unique: true?

At least that is one reason I know of for the error you are getting.

[Error: E11000 duplicate key error collection: freeCodeCamp_task.people index: name_1 dup key: { name: "Mary" }]

Did you try to do what I said? Delete the collection (click the delete icon next to the collection) then try the submission again.

I encountered same error, and after deleting the collection and re-running, it worked for me. No idea why it works now, Can someone explain?

Yes, I had unique: true in my Schema. I had to created another collection and that worked…thanks.