I am trying to do a regular expression search on e-mail addresses, but I am not sure if I am doing the query correctly. The email addresses are stored as an array so I am trying to build the selector as
I am doing the search using Meteor.publish()
so it should have no restrictions.
{ 'emails[0].address' : new RegExp(email) }
I also tried
{ 'emails.address' : new RegExp(email) }
{ "emails.address' : { $regex: /pattern/i } }
The “i” is for case-insensitive searches.
https://docs.mongodb.org/manual/reference/operator/query/regex/#op._S_regex
Thanks, I used a slightly different variation which uses
{ 'emails.address' : { $regex: 'pattern', $options: 'i' } }
That’s actually the same thing.
The difference is that I can pass pattern as a string rather than as a RegExp object. But the example is the same.
You have to take care when you do this because you’ll run into problems as soon as you use e.g. a dot or a dash in your string (which will happen fast when you are searching for email addresses).
Don’t forget to escape those special characters.
agreed I used lodash.escapeRegExp
for that purpose