MongoDB and Mongoose - Delete Many Documents with model.remove()
See original GitHub issueAffected 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
Mongoose 5.4.13:
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:
- Created 2 years ago
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
I’ll create the PR 👍🏽