Q: How do I use regular expressions (regex) in MongoDB and Studio 3T? MongoDB seems to have two different ways of writing regular expressions too and I can’t find a “match” option in the Visual Query Builder.
Welcome to the latest Studio 3T Ask Manatees Anything – #Studio3T_AMA. Want to know how to use Studio 3T to make your MongoDB life better? Just tweet and use #Studio3T_AMA or email [email protected].
A: It’s true that MongoDB can be somewhat opaque as to what is the right format for regular expressions, or regex for short. And as knowing regular expressions can be a superpower to your searching, that’s something we need to clear up.
What are Regular Expressions?
Let’s start with a quick explainer as to what regular expressions are.
Regular expressions are strings which describe a way of matching another string. At its simplest, a regular expression is a simple string like “aaa”, which would match with “aaaloha” or “haaallo”. A plus “+” says match with one or more of the preceding character. So “a+” will match one or more of the letter “a”.
An asterisk “*” is like a “+” except it matches with none or more of the preceding character. Also, a “.” in a regular expression will match any character. That makes “.*” the same as any number of any character – great for skipping text you don’t need to match with. There’s a whole world of other special characters that can be used in regular expressions. Used together, you can create expressions that match complex text patterns and match more precisely than ever.
For an introduction to regular expressions, see sites such as regexone.com or regular-expressions.info and their tutorials.
Back to MongoDB and regex. MongoDB uses PCRE regex – Perl Compatible Regular Expressions. There’s no Perl needed to use them though; it’s just one of the most widespread and popular variants of regular expressions out there.
There are two ways of representing a regular expression in MongoDB, both sort of inherited from the world of JavaScript.
Literally MongoDB Regex
First, there’s the regular expression object, also known as the regular expression literal in JavaScript. It is written as /regularexpression/options without quotes, the “/” starts and ends the expression string.
It can also be followed by options – single characters which tell whatever is processing the expression how to handle it. The most common is the option “i” which tells it to be case insensitive, though options are optional.
If we wanted to match with “private” or “Private” or “PRIVATE” in a field, we can use a regexp like: /private/i and that would look like this in a query:
db.listingsAndReviews.find({ name: /private/i });
This style of regular expression is a native datatype of BSON and can turn up on its own in $in statements too. This has been the default way of doing regular expressions in MongoDB for a long time.
Expressively MongoDB Regex
Then there’s the $regex JSON expression. That JSON part is important, because by the spec, you can’t have a literal regex in JSON. So, MongoDB’s $regex operator takes on that task. Using this format, our query above looks like this:
db.listingsAndReviews.find({ name: { $regex: "private", $options: "i" } });
The regular expression is now split over two fields, $regex which contains the expression and an optional $options which carries the options for the expression. This version doesn’t become a native data type, but it can be used in JSON files.
Visually Regular Expressions
We’ve found the most common use of regular expressions is to test if a string contains another string. In Studio 3T’s Visual Query Builder, you can find the “contains” operator which takes your string value and wraps it in a regular expression. So this setting in the Query Builder:
Becomes this query in the text version of the query.
If you want to enter your own regular expression, the next stop is the Regex field option. In the Visual Query Builder’s Query, we can set the field to match with and set the operator to Equals. Now, click on the parameter type icon and a menu will pop up. Select RegEx from the list.
Our parameter is now going to be a regular expression and we can see “//” in the parameter field. We can’t edit it directly though. Click on the … button to the right and the regular expression editor is displayed:
In this editor you can type your regular expression in the text area and select the options you want enabled when the expression is processed. When you are done, click OK and you’ll return to the query builder where you can see the literal version of your expression.
Custom Regex
There is one other way to create a regular expression in the Visual Query Builder: You can use the (custom) option. This lets you type in any MongoDB query expression and have it included in the query. Be aware that if you don’t format your query expression correctly, it could make the whole query fail to run. So, just type your literal regular expression in:
And you’ll be doing a regular expression match against that literal expression.
Now, go forth and use the power of regular expressions and level up your matching game with Studio 3T.