I want to delete all document record from all collection from my database.
Asked
Active
Viewed 6,489 times
2 Answers
3
You can delete all document in mongodb by simply using db.collectionName.remove( { } )
But if your are using node.js, delete all documents by following way.
MongoClient.connect('mongodb://localhost/mochatests', function(err, db) {
db.collection('contacts', {}, function(err, contacts) {
contacts.remove({}, function(err, result) {
if (err) {
console.log(err);
}
console.log(result);
db.close();
});
});
});

Radhesh Vayeda
- 881
- 7
- 20
-
1"DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead." I used deleteMany instead of remove. Eg: db.collectionName.deleteMany ( { } ). Thanks @Radhesh for the original answer. – Nhon Ha Apr 26 '20 at 09:59
-
this delete one from one collection and not all collections – Cfir TSabari Dec 13 '20 at 07:16