Model.remove() give me problem! :(

Hi, i’m studying this lesson, i wrote the following code, following this hint

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

now, i think i have to remove more than only one name, that in this case is “Mary”, i’ve tried to replace it with “Frankie” that’s a name present in my var arrayOfPeople that i inserted first in the code due to a previous lesson but doesn’t change anything, now my question is: do i have to remove more than the only name “Mary”? and if yes, how? do i have to change const nameToRemove filling it with an array of strings including more than one name? hope for your help, thank you.

I’ve noticed that in my database there are two objects, both with name: “Mary”, but with different array of favourite foods and age, i think these two object must be removed, but with that code it doesnt’t work, hope you can show me the way

UPDATE: i’ve checked my mongodb’s databse, the two objects including name:“Mary” are disappeared, but in the fcc’s lesson when i put my replit’s app link it doesn’t let me pass the lesson. :thinking:

Hello there,

Would you mind sharing a link to your code on Replit?

I would suggest you install the same versions of the dependencies as required in install and set up mongoose. Then delete the people collection from the DB and try again.


I won’t link to your Replit because you still have the connection string with the password in it.

  1. Click the Secretes button.

  2. Add MONGO_URI as the key

  3. Add your DB connection string without the surrounding quotes as the value.

  4. Access the connection string using process.env.MONGO_URI and remove the literal string from your code.

2 Likes

That worked! Thank you! So sofisticated this back-end development :joy:

Link edited in that post! I need to study cybersecurity i think :joy:

@mtssmoreno Your code is passing for me.

Your link is passed now!Could you share me you code?

Thank you. I have the same problem and your solution works for me

I try it but it doesn’t work, pls help me.

Without seeing your code we can’t help. Post your Replit.

It might be better if you opened your own thread.

1 Like

Doesn’t work.

Test output: Failed:Deleting many items at once should succeed

My replit: https://boilerplate-mongomongoose.webdev4422.repl.co

Console output: (node:1542) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.

If change Person.remove() to Person.deleteMany() test not passing.

Also tried: clean browser cache, change different mongoose versions, test publish on other platforms.

Also on :

GitHub: https://github.com/webdev4422/boilerplate-mongomongoose/blob/main/myApp.js

GitHub Pages: https://webdev4422.github.io/boilerplate-mongomongoose/views/

I had the same issue, after editing all the Person.remove() and p.remove() functions in the server.js file it worked for me

Person.remove() =>>Person.deleteOne
p.remove() =>>p.deleteOne

2 Likes

Uhhh this is just a note so it may be wrong cause I never know what I’m doing but I believe Mongoose has Deprecated the .remove() method.

So I did CTRL+F to try and find other methods that were described as .remove() in the documentation. Sure enough the .deleteMany() method had this in it’s description, " Behaves like remove(), but deletes all documents that match conditions regardless of the single option.".

Again I don’t know if this particular method does what we were trying to accomplish exactly but maybe this idea leads someone to find a correct answer. Also, at the end of the day my code still passed using this method.

This problem happened to me. I tested it locally and it didn’t return any errors with both Model.remove and Model.deleteMany, but on the platform it gave an error several times.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.