0
0
MongoDBquery~5 mins

Regex queries in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of using regex queries in MongoDB?
Regex queries in MongoDB are used to search for documents where a string field matches a specific pattern, allowing flexible and partial text matching.
Click to reveal answer
beginner
How do you write a basic regex query in MongoDB to find documents where the 'name' field starts with 'A'?
Use the query: { name: { $regex: '^A' } } which matches any 'name' starting with the letter 'A'.
Click to reveal answer
beginner
What option do you add to a MongoDB regex query to make it case-insensitive?
Add the option '$options: "i"' to the regex query. For example: { name: { $regex: 'abc', $options: 'i' } } matches 'abc', 'Abc', 'ABC', etc.
Click to reveal answer
intermediate
Explain the difference between using $regex and $text in MongoDB queries.
$regex matches string patterns using regular expressions, good for flexible pattern matching. $text searches text indexes for words or phrases, optimized for full-text search but less flexible for patterns.
Click to reveal answer
intermediate
What is a potential performance consideration when using regex queries in MongoDB?
Regex queries that do not start with a fixed prefix can cause full collection scans, which are slow. Using anchored regex (like '^pattern') helps MongoDB use indexes and improves performance.
Click to reveal answer
Which MongoDB operator is used to perform regex pattern matching?
A$regex
B$text
C$match
D$search
How do you make a regex query case-insensitive in MongoDB?
AAdd $options: 'i'
BAdd $case: true
CUse $text instead
DUse $ignoreCase: true
What does the regex pattern '^abc' match in MongoDB?
AStrings ending with 'abc'
BStrings starting with 'abc'
CStrings containing 'abc' anywhere
DStrings exactly equal to 'abc'
Which of the following is a performance tip for regex queries in MongoDB?
AUse unanchored regex for faster queries
BAvoid using indexes
CAlways use $text instead of $regex
DUse anchored regex patterns starting with '^'
What is the difference between $regex and $text in MongoDB?
AThey are the same
B$regex is faster than $text
C$regex matches patterns; $text searches text indexes
D$text uses regex internally
Describe how to write a MongoDB query using regex to find documents where a field contains a specific substring, ignoring case.
Think about how to match anywhere in the string and ignore uppercase/lowercase differences.
You got /3 concepts.
    Explain why using unanchored regex patterns in MongoDB queries might affect performance and how to improve it.
    Consider how MongoDB uses indexes with regex patterns.
    You got /4 concepts.