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?
✗ Incorrect
The $regex operator is used to match string fields against regular expression patterns.
How do you make a regex query case-insensitive in MongoDB?
✗ Incorrect
Adding $options: 'i' makes the regex match case-insensitive.
What does the regex pattern '^abc' match in MongoDB?
✗ Incorrect
The '^' anchors the pattern to the start of the string.
Which of the following is a performance tip for regex queries in MongoDB?
✗ Incorrect
Anchored regex patterns allow MongoDB to use indexes and improve query speed.
What is the difference between $regex and $text in MongoDB?
✗ Incorrect
$regex matches string patterns; $text searches full-text indexes for words or phrases.
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.