Mongoose - Delete Many Documents with model.remove()

Mongoose - Delete Many Documents with model.remove()
0

#1

Hi,

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

Any guides or examples appreciated

K


#2

Hey @clickingmouse -> Look here:


#3

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)
}
)};

#4

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.


#5

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


#6

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.


#7

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.


#8

Yes, I also think so.


#9

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/);
};`


#10

Thanks so much. That worked!