Currently, I am implementing the following $regex
pattern in a mongo query:
{domain: {$regex: '^(.+\.)?youtube.com$'}}
I anticipate that it will match youtube.com
as well as sub.youtube.com
.
However, the issue I have encountered is that it also matches values like justyoutube.com
.
Interestingly, in JavaScript, the match does not occur:
console.log(/^(.+\.)?youtube.com$/.test('justyoutube.com'));
// This returns `false` as expected.
Do you have any suggestions on improving this functionality? Is the problem related to my regex itself, or is it due to the regex library utilized by MongoDB?
Update: I have noticed that utilizing /pattern/
instead of 'pattern'
yields the desired outcome. However, I am still interested in finding a solution using quotation marks for easier debugging in MongoDB Compass.