Ankit
Ankit

Reputation: 1887

Ignore special characters before match conditions

How can I write equivalent of following in mongo? I need to ignore some characters(spaces, hyphen) from a particular column before conditions are checked. For the sake of putting an example of mysql I am just removing space.

select * from TABLE
where REPLACE('name', ' ', '') = 'TEST'

So if name column has " T E S T" that should match.

Upvotes: 2

Views: 2243

Answers (1)

dgiugg
dgiugg

Reputation: 1334

You can try with $where operator in your query:

{$where: "this.name.replace(/[ -]/g,'') == 'TEST'"}

or:

{$where: "this.name.match(/T[ -]*E[ -]*S[ -]*T/)"}

or directly a $regex:

{name: /T[ -]*E[ -]*S[ -]*T/}

More info about $where $regex operators.

Upvotes: 5

Related Questions