How to use regex in MongoDB $ne condition
I am trying to query my collection where a particular field does not match a given regex. I am trying with the following query
db.getCollection('users').find({email: {$ne: /^.*gmail\.com$/}})
But I am getting following error
Error: error: {
"ok" : 0,
"errmsg" : "Can't have regex as arg to $ne.",
"code" : 2,
"codeName" : "BadValue"
}
1 Answer
4 years ago by VD
You can use $regex and $not
to perform your query.
Try the following
db.getCollection('users').find({email: {$not: /^.*gmail\.com$/ } })
4 years ago by Karthik Divi