Using $regex mongodb query in node js not working Using $regex mongodb query in node js not working mongodb mongodb

Using $regex mongodb query in node js not working


The $regex value needs to be either the string pattern to match or a regular expression object. When passing a string pattern, you don't include the / delimitters like you're doing in your node.js code.

So either of these would work in node.js:

var selector = {"displayName": {$regex: ".*abc.", $options:"i"}}

OR

var selector = {"displayName": {$regex: /.*abc./, $options:"i"}}


could you please try this one, It works for me.

var getValue = "abc";db.users.findOne({ displayName: new RegExp(regexValue, "i") }, function(err, res) {  if (err) {    console.log(err);  } else {    console.log(res.length);  }});


In mongodb consoletry this query

db.users.findOne({"displayName":{$regex: ".*abc", $options:"i"}})

and in nodejs var selector = users.find({ "displayName": {$regex: ".*abc", $options:"i"}})

here users = is a collection name