In this blog post i am going to explain how to delete MongoDB document. The are few ways which are illustrated below :
If we execute remove on collection without any params then it will delete all records
1 2 3 4 |
[php] db.users.remove({}) // it will delete all documents along with users collection it will keep the indexes. Think of it as DELETE in MySQL [/php] |
Drop is used to delete a collection and it’s indexes as well.
1 2 3 4 |
[php] db.users.drop() // it will delete users collection and all the indexes. Think of it as DROP TABLE in MySQL [/php] |
We can also delete selective document based on condition. Following examples illustrate the same :
1 2 3 |
[php] db.users.remove({age:'28'}); // it will only delete all documents where age is 28 [/php] |
We can also use other operators for deleting documents which are explained as follows :
1 2 3 4 5 6 7 |
[php] db.users.remove({age:{$gt:'28'}}); // it will only delete all documents where age is greater than 28 db.users.remove({age:{$lt:'28'}}); // it will only delete all documents where age is less than 28 [/php] |
You can find other post in MongoDB series at this link
Recent Comments