[MongoDB and Mongoose] Perform new Updates and Delete many documents exercises

[MongoDB and Mongoose] Perform new Updates and Delete many documents exercises
0

#1

Perform New Updates on a Document Using model.findOneAndUpdate()

Perform New Updates on a Document Using model.findOneAndUpdate()
It doesn’t pass
My code:

var findAndUpdate = function(personName, done) {
  var ageToSet = 20;
  Person.findOneAndUpdate({name: personName}, {$set: {age: ageToSet}}, {new : true}, function(err, data){
    done(null, data);
  });
};

Delete Many Documents with model.remove()

I have no idea what to do here. My code is below. Don’t know what to pass into done and dont know how to test output with glitch.

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

It takes document query, which I provided, callback and options for single deletion which is not instructed.

callback data doesn’t provided deleted document so I know it is wrong, but I do not know how to get deleted document from the data it provides to pass it into done


Mongoose - Delete Many Documents with model.remove()
#2

Hi, Did you ever solve it?


#3

Hey @jm18457 and @clickingmouse

What’s missing from the “Delete Many Documents with model.remove()” is the callback to return the results.

Your code from above:
var removeManyPeople = function(done) {
  var nameToRemove = "Mary";
  Person.remove({name: nameToRemove}, (err, data) => done(null, data));
};

…should be…

Code that passes tests:
var removeManyPeople = function(done) {
  var nameToRemove = "Mary";
  
  Person.remove({ name: nameToRemove }, function(error, data) {
    error ? done(error) : done(error, data);
  });
};

To pass the tests, your function has to say, "Was there an error? If so, call .done() with the error as a parameter. If there was no error, call .done() with the data and the parameter.

Hope this helps. Happy coding.


#4

Hi when I run this code "var removeManyPeople = function(done) {
var nameToRemove = “Mary”;

Person.remove({ name: nameToRemove }, function(error, data) {
error ? done(error) : done(error, data);
});
};"

I get this error “removeMany is not a function”, please guide me as to why am I getting this error.


#5

It might be that in your success callback you have ‘error’ instead of ‘null’. It should be
error ? done(error) : done(null, data);
instead of
error ? done(error) : done(error, data);

you might also try scrolling to the bottom of the file to make sure this line appears like this:
exports.removeManyPeople = removeManyPeople;