$regex operator

Question 1: Is $regex operater, by any chance made obsolete??

I run the following, and get an output of 83 (which is correct)

I modify the above query and fire it without the $regex operator, and get the same results.

We are able to search on regular expressions, without using the $regex operator. So is it right to assume that the $regex operator is obsolete?

Question 2: Moreover, why is $regex operator not working in Compass?

I put the following filter, and my FIND button remains disabled. (though screenshot doesn’t show it is disabled, but trust me it is disabled)

I change my find to the following and FIND button gets enabled, and when clicked, returns 83 matches as expected.

So based on above two,
(1) $regex operator doesn’t seem to work in Compass.
(2) Although it works in Mongo Shell, but seems to be obsolete.

Request you to please explain this behavior with $regex operator.

1 Like

Hi @PuneetC,

$regex operator is not obsolete in MongoDB.

Here you are are using MongoDB’s Perl compatible regular expressions (i.e. “PCRE” ).

For more details on the same please refer to the following documentation:

Here, your find button is disabled because “awards.title” is not a field in the document.

The correct query for Compass is as follows:

{“awards.text”: {$regex: ‘^Won.*’} }

You need to remove slash here as you are already using $regex operator and enclose the pattern in single quotes.

I hope it solves your query.

Please let me know, if you have any further doubts.


Following is the screenshot from the Lesson on $regex operator by Shannon. It uses slashes with $regex operator.

Hence, I was trying to use the exact same format in Compass as follows, but the FIND remains disabled.

Yes, but replacing the slashes with single quotes, works as follows

So it looks like $regex operator works with slashes in Mongo Shell, but in Compass, we need to use $regex operator with single quotes?

Moreover, following query (without $regex operator) makes FIND button in Compass enabled, and fetches the desired results. So isn’t $regex operator unnecessary / redundant, as same can be achieved without the use of $regex operator?

1 Like

@Sonali_Mamgain can you please comment on this…

Hi @PuneetC,

The usage of regex in Compass is different. You can use the following query filter in Compass using $regex operator:

{“awards.text”: {$regex: “Won”}}

This is equivalent to the following filter which uses regular expression objects (/pattern/) :

{“awards.text”: /^Won .*/}

Please refer to the following doc for understanding the usage of $regex and /pattern/ syntax:

Please let me know, if you have any further questions.