I have not find a method to do it yet. If we can not remove documents using regex expression, what can we do to make it done? And why does not mongodb provide such a driver ?
MongoDB uses Perl compatible regular expressions (i.e. "PCRE" ) version 8.42 with UTF-8 support. For restrictions on particular syntax use, see $regex vs. /pattern/ Syntax. The following <options> are available for use with regular expression.
To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.
In MongoDB, you can use the $unset field update operator to completely remove a field from a document. The $unset operator is designed specifically to delete a field and its value from the document.
The .remove()
method just takes a query object, so regular expressions are just a standard query for MongoDB:
db.collection.remove({ "field": /^string/ })
Removes anything that has "field" that starts with "string"
Look at the documentation for $regex
as well.
Documents can be remove using remove()
method and $regex
query.
For example
db.users.remove({"email" : { $regex : /$test/}})
It will remove all user email starts with 'test'.
db.users.remove({"email" : { $regex : /@yopmail.com$/}})
It will remove all user email ends with 'yopmail.com'
Details are explained here, mongoDB official site
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With