Of course you can just reverse with $not
:
db.collection.find({ "name": { "$not": /am/ } })
But since it's a $regex
you can just do this:
db.collection.find({ "name": /^((?!am).)*$/ })
Or
db.collection.find({ "name": { "$regex": "^((?!am).)*$", "$options": "gm" } })
Making sure that the content matches over multiple lines if your string is formed that way.
For multiple fragments to exclude you can separate them in the expression using |
. For example to exclude words containing "bo" or "am":
db.collection.find({ "name": { "$regex": "^((?!bo|am).)*$", "$options": "gm" } })
Or use the $nin
operator ( "not" $in
) which accepts regular expressions:
db.collection.find({ "name": { "$nin": [/bo/,/am/] } })