Using $regex mongodb query in node js not working

javascript, mongodb, node.js, regex

Solution

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"}}

Problem

From node js, i trying to retrieve the documents based on the keyword with case-insensitive. When i try directly from the mongo db console , i am able to see the result. ``` db.users.findOne({"displayName":{$regex: /.*abc./, $options:"i"}}) ``` But when i try the same in node js, i am getting empty result. ``` var selector = { "displayName": {$regex: "/.*abc./", $options:"i"}} ``` is this due to regular expression not in javascript. Can anyone please help me in this.

Original source

Related problems