codeofnode
codeofnode

Reputation: 18609

Exact search in mongodb 2.6, just only case insensitive

Using regex = { name : { $regex : inputName, $options: 'i' } }

even when input name is blank, the query returns the first document. Even when input is a, query returns the first document which have a anywhere in the name.

I want if then input name is "jo", then it should only return the first document either with name "Jo", "JO", "jO", "jo".

Please remember i recieve inputName as variable

Upvotes: 2

Views: 446

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Use a JS RegExp constructor to build a regexp dynamically:

new RegExp("^" + inputName + "$", "i")

The i modifier will provide case insensitive matching and ^ / $ anchors will make sure the full string match will be requried.

Upvotes: 2

Related Questions