MongoDB
Delete document
How to delete a document from collection using NodeJS
Remove multiple documents:
/* Send error to browser */
var errSend_browser = function (err, res) {
res.send('category_model.js -:' + err);
};
//var selector = {id: 1}; //remove only one document
var selector = [{id: 1}, {id: 5}]; //remove two documents
var options = {};
db.collection('collection_name', function (err, collect) {
if (err) { errSend_browser(err, res); }
collect.remove(selector, options, function (err, result) {
if (err) { errSend_browser(err, res); }
res.redirect('/admin/categories/');
console.log('\nDeleted records: ' + result); //{"ok":1,"n":2}
});
});
Remove one document:
var selector = {id: 1};
db.collection('collection_name').remove(selector, options, function (err, status) {
if (err) {
errSend_browser(err, res);
} else {
res.redirect('/admin/categories/');
console.log('\nDeleted records: ' + status); //{"ok":1,"n":1}
}
db.close();});