Reputation: 349
I am using Mongo Aggregators to prepare queries.
I would like to check two things:
I am trying to use the $regex method but It seems I have to specify too many fields:
{ value: { $regex: "!" } }
This only finds strings that contains "!", but I would like to find strings that may (or may not) contains more special chars.
Upvotes: 0
Views: 256
Reputation: 14317
1. If my string has digits (e.g. "Luk3 Sk1w8lk3r");
Your filter {"value":{"$regex": "\d"}}
.
2. If my string contains special characters (e.g. "L*ke Sk!w"lz")
{"value":{"$regex": "[.*+?^${}()|][\]"}}
The regex can be written little differently (without using the escape \
for all special characters):
"[.*+?^${}()|\]\[\\]"
You specify the escape \
for [
, ]
and \
. Further, you can include other special characters like !
and '
within the regex.
3. Or if contains both (e.g. "L0ke Sk!w0l'z8")
Here are regex for both:
"(?=.*[.'!*+?^${}()|\]\[\\])(?=.*[0-9])"
"[.'!*+?^${}()|\]\[\\]|\d"
Upvotes: 2
Reputation: 349
I found workaround using $match:
{"value":{"$regex": "\d"}}
{"value":{"$regex": "[\.\*\+\?\^\${}\(\)|\]\[\\]"}}
Appreciated every future improvement on these queries!
Upvotes: 1