Mongoose - Delete Many Documents with model.remove()

Hi,

Not really sure how to pass this challenge, I feel like, i’ve tried many combinations already

Any guides or examples appreciated

K

Hey @clickingmouse → Look here:

In console(Status button) is possible to see:
collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.

Instead Person.remove should be Person.deleteMany
In case of error is at the end condition - ternary operator or if statement
whole solution:

var removeManyPeople = function(done) {
  var nameToRemove = "Mary";
  Person.deleteMany({name: nameToRemove}, (err, data)=> {
  err ? done(err) : done(null,data)
}
)};
2 Likes

Having the same problem here. Tried many different ways without any success.
Here is my code

var removeManyPeople = function(done) {
  var nameToRemove = "Mary";
  Person.deleteMany({name: nameToRemove}, function(err, data) {
    if (err) {
      done(err);
    } else {
      done(null, data);
    }
  });
};

Any hints or suggestions would highly be appreciated.

2 Likes

I have solved the problem by using new database from mlab. Somehow, the old database that I was using was not working properly.

1 Like

I forgot what I did but there are definitely answers in the documentation and stack-overflow. There are different options just try them and you will find one that works.

Thanks so much. I was stuck on this for a long time. I couldn’t figure out what I was doing wrong. Finally I took your suggestion and decided to use a new database and it worked.

In my original schema I used unique for name. Once I eliminated that, I thought it would work, but it didn’t. I’m still not sure if that affected it.

Thanks again.

Yes, I also think so.

I also think if you have repetition of/more done(null, data); on the same page. It makes it fail. I got the same issue but I removed one and it passed no need for a new database.
`var removeManyPeople = function(done) {
var nameToRemove = “Mary”;
Person.deleteMany(
{name: nameToRemove},
(err, data) => {
if (err) {
done(err);
}
done(null, data);
}
)

//done(null/, data/);
};`

1 Like

Thanks so much. That worked!

1 Like

If you’ve defined the schema where the ‘name’ field is set with ‘unique’ index. At solution validation, you’d get this error:
Error: E11000 duplicate key error collection: test.people index: name_1 dup key: { name: “Mary” }

The reason is fCC stubs a couple records (documents) with the same ‘name’ value (Mary).
e.g [ { name: ‘Mary’, age: 16, favoriteFoods: [ ‘lollipop’ ] },
{ name: ‘Mary’, age: 21, favoriteFoods: [ ‘steak’ ] } ]

Fix to pass validation:

  • comment-out ‘unique: true’ of the schema
  • remove index (Drop Index button) of the ‘name’ in DB Collections: mongoDB/Cluster/Collections/Indexes

Note:
Model.remove() or Model.deleteMany() should both work

Thanks a ton!!!

try using this it worked on one try

var removeManyPeople = function(done) {
var nameToRemove = “Mary”;
Person.remove({name:nameToRemove},(err,data)=>{
if(err)
return console.log(err);
done(null,data);
});
};