Reputation: 2139
I have a quick question. I'm updating functionality behind a small search box on my application, it searches for people based on either their first or last name.
It works fine the way it is, but I'm trying to actually expand the functionality of it to include some common international characters, like í or ö for example.
Using the name Martin as an example, in for use with PHP's preg_match() function, I'd write my regular expression something like this: @Mart[ií]n@i
to match Martin or Martín.
Is bracket notation like this something that can be written into a MongoDB regular expression? I know their regex notation isn't as robust as PHP's, but I haven't seen anything one way or another about using brackets in this fashion.
Thanks!
Upvotes: 1
Views: 1075
Reputation: 7465
MongoDB uses Perl Compatible Regular Expressions (PCRE's). You are able to write a regex using bracket notation for this query. In the example you gave, to find a user with the given name Martín
, you can do something like:
> db.users.insert({name:'Martín'});
WriteResult({ "nInserted" : 1 })
> db.users.find({name:{$regex:'Mart[ií]n'}});
{ "_id" : ObjectId("..."), "name" : "Martín" }
This would return the record you want.
PHP uses PCRE's, so using the same types of queries should accomplish what you want.
Upvotes: 1