Stuck on "Delete Many Documents with model.remove() " ...in need of suggestion

Does anybody passed this challenge recently?
I tried different methods but failed…

Your project link(s)

solution: boilerplate-mongomongoose - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36

Challenge: Delete Many Documents with model.remove()

Link to the challenge:
https://www.freecodecamp.org/learn/back-end-development-and-apis/mongodb-and-mongoose/delete-many-documents-with-model-remove. My code

1st option

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

2nd option

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

3rd option

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

I see this error in the response in the network tab (browser dev tools).

E11000 duplicate key error collection: fcc_challange.people index: name_1 dup key: { name: "Mary" }

Not sure if it’s related but I would suggest you start by using the versions of the packages you are asked to in the starting challenge.

It doesn’t look like an error in the code (works for me). Maybe try manually cleaning the collections on mongodb atlas before submitting again (select the project from the dropdown and click the Browse Collections button, you can then manually delete stuff).

Thank you very much for your info. I downgraded the versions of Mongoose and MongoDB what fcc asked and deleted the previous collections. Problem is still unchanged.

If I fork your code and add my own DB it passes the test. So I’d think it must be the DB. Did you delete everything? Or maybe try deleting the DB completely and creating a new one.


BTW, your mongodb version is still a major version above the one asked for (mongodb@~3.6.0). But as said the fork worked for me.

2 Likes

Yes, now I tried with older versions followed by fcc. Unfortunately, no luck… I deleted the collection in DB. Also tried by opening another account in MongoDB.

1 Like

The fact that I can fork your code and it passes must mean one of two things.

  1. The dependencies are not updated correctly and forking the project reinstalls the dependencies. Try forking your own project.

  2. The DB is the problem. Try creating a new DB.

You do not need a new account. Just create a new DB.

2 Likes

Tried…still same situation

1 Like

If my DB doesn’t work how I can see the data in the collection? I can see the data of “Mary” there.

1 Like

Edit: Sorry I didn’t notice your Schema. It is the unique: true option. Delete the “people” collection and remove that option. It now should work with remove and keep working.

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

I don’t see a fork in your “All repls” list?

I just test it again with the same DB and now I get the same error. So I looked in the DB and I still see one of the “Mary” documents under the “people” collection. If I delete the “people” collection it passes again, I then ran it again it passed, I then ran it again and it failed.

If I switch to deleteMany instead it seems to keep working and still passes the test. You still have to delete the “people” collection first.

I don’t know enough about mongodb to give you a good explanation but the change to Unique Indexes might be related.

1 Like

Well, now it passed. I deleted ‘people’ 3 times in DB manually…then it worked. Really, didn’t understand what’s the reason behind this ?

The test is creating two docs both with the name Mary.

(getUserInput) =>
  $.ajax({
    url: getUserInput('url') + '/_api/remove-many-people',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify([
      { name: 'Mary', age: 16, favoriteFoods: ['lollipop'] },
      { name: 'Mary', age: 21, favoriteFoods: ['steak'] }
    ])
  }).then(
    (data) => {
      assert.isTrue(!!data.ok, 'The mongo stats are not what expected');
      assert.equal(
        data.n,
        2,
        'The number of items affected is not what expected'
      );
      assert.equal(data.count, 0, 'the db items count is not what expected');
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

If you keep the server running as you delete the collection it will work, but if you restart the server it will fail again. There also seems to be some weird timing issue because if I use Postman it seems like I can keep getting it to pass after deleting the collection (fairly consistently), but if I use the submit form it fails after two or three tries.

1 Like