question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoDB and Mongoose - Delete Many Documents with model.remove()

See original GitHub issue

Affected page

The lesson at https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/delete-many-documents-with-model-remove doesn’t work with mongoose 6+ because now it just returns the deletedCount, which causes the code from server.js, lines 343 through 351, to fail.

Mongoose 6.0.1 image

Mongoose 5.4.13: image

This would fix it:

if (data.ok === undefined) {    
    try {
        if ("object" === typeof data && Object.keys(data).length === 1) {
            // for mongoose v6
            data.ok = true;
            data.n = data.deletedCount;
        } else {
            // for mongoose v4
            data = JSON.parse(data);
        }
    } catch (e) {
        console.log(e);
        return next(e);
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
13cbenjamincommented, Aug 26, 2021

I would like to create a PR for this as I am a first time contributor and I reported this issue on the FCC forum. It will be a few hours until I can submit. I understand these are not assigned so if it’s still open when I’m home, I’ll gladly take it as my first contribution.

1reaction
skaparatecommented, Aug 26, 2021

I’ll create the PR 👍🏽

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose - Delete Many Documents with model.remove()
In console(Status button) is possible to see: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead. Instead Person.
Read more >
Mongoose | deleteMany() Function
This function behaves like the remove() function but it deletes all documents that match conditions regardless of the single option.
Read more >
The deleteMany() Function in Mongoose
The deleteMany() function is how you can delete multiple documents from a collection using Mongoose. It takes up to two parameters:.
Read more >
db.collection.deleteMany()
db.collection.deleteMany() throws a WriteError exception if used on a time series collection. To remove all documents from a time series collection, ...
Read more >
Mongoose - remove multiple documents in one function call
I believe what youre looking for is the $in operator: Site.deleteMany({ userUID: uid, id: { $in: [10, 2, 3, 5]}}, function(err) {}).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found