Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

How do I use MongoDB regex (regular expressions) in Studio 3T? #Studio3T_AMA

Posted on: 22/09/2021 (last updated: 29/09/2021) by Dj Walker-Morgan

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:

The contains operation in the Visual Query Builder actually creates a MongoDB regex
The Visual Query Builder’s Query for Contains

Becomes this query in the text version of the query.

The inline version of the Contains operation is a literal regex
The inline version of the Contains operation is a literal regex

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.

Selecting RegEx for a match in MongoDB
Selecting RegEx for a MongoDB match in Studio 3T

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:

The Regular Expression editor in Studio 3T
The Regular Expression editor in Studio 3T

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:

Using the Custom operation to directly enter a literal MongoDB Regex
Using the Custom operation to directly enter a literal MongoDB Regex

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.


How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Dj Walker-Morgan

Dj has been around since Commodore had Pets and Apples grew everywhere. With a background in Unix and development, he's been around the technology business writing code or writing content ever since.

Related articles

  • Regular Expressions in Studio 3T
  • The Value of a Regular Expression: #Studio3T_AMA
  • I’ve been invited to use Studio 3T – What should I do next?
  • Working with MongoDB Data? Use These Data Masking Techniques
  • Exercise 2: Exporting a collection as a .csv file for use by a third-party tool

Tags

2022 academy aggregation AMA atlas Certification christmas community connections culture date tags events export features hackolade import intellishell In Use JSON knowledge base migration modelling mongodb mongoodb mongosh My 3T productivity query regex releases schema security SQL Studio 3T tasks time series tips updates webinar windows

Browse by MongoDB topic

  • Connecting to MongoDB
  • Database Administration & Security
  • Getting Started with MongoDB
  • Getting Started with Studio 3T
  • Import/Export
  • Job Automation & Scheduling
  • MongoDB Aggregation Framework
  • MongoDB/Studio 3T Workshops
  • Performance
  • Query & CRUD Operations
  • Reference
  • Schema
  • Studio 3T Licensing
  • Support and other resources
  • Working with MongoDB & SQL
  • Working with MongoDB Atlas

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2023 3T Software Labs Ltd. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum

We value your privacy

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
Accept all
Decline all
Manage cookies
✕

Privacy Preference Center

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Google Analytics

Google Ads

Bing Ads

Facebook

LinkedIn

Quora

Hotjar

Reddit

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

HubSpot

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected