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
✗ 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?
AAdd $options: 'i'
BAdd $case: true
CUse $text instead
DUse $ignoreCase: true
✗ Incorrect
Adding $options: 'i' makes the regex match case-insensitive.
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'
✗ Incorrect
The '^' anchors the pattern to the start of the string.
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 '^'
✗ Incorrect
Anchored regex patterns allow MongoDB to use indexes and improve query speed.
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
✗ 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.
Practice
(1/5)
1. What does the $regex operator do in a MongoDB query?
easy
A. It searches for documents where a field matches a text pattern.
B. It sorts documents in ascending order.
C. It updates documents with new values.
D. It deletes documents matching a condition.
Solution
Step 1: Understand the purpose of $regex
The $regex operator is used to find text patterns inside string fields in MongoDB documents.
Step 2: Compare with other operations
Sorting, updating, and deleting are different operations and do not use $regex.
Final Answer:
It searches for documents where a field matches a text pattern. -> Option A
Quick Check:
$regex = pattern search [OK]
Hint: Remember: $regex means 'match pattern' in text fields [OK]
Common Mistakes:
Confusing $regex with sorting or updating operations
Thinking $regex modifies data instead of searching
Assuming $regex works on non-string fields
2. Which of the following is the correct syntax to find documents where the field name starts with 'A' using regex in MongoDB?
easy
A. {"name": {"$regex": "A$"}}
B. {"name": {"$regex": "A*"}}
C. {"name": {"$regex": "^A"}}
D. {"name": {"$regex": "[A]"}}
Solution
Step 1: Understand regex anchors
The caret ^ means the start of the string, so ^A matches strings starting with 'A'.
Step 2: Check other options
A$ matches strings ending with 'A', A* matches zero or more 'A's anywhere, and [A] matches any 'A' character anywhere.
Final Answer:
{"name": {"$regex": "^A"}} -> Option C
Quick Check:
Start with 'A' = ^A [OK]
Hint: Use ^ at start of pattern to match beginning of string [OK]
Common Mistakes:
Using $ instead of ^ to match start
Misunderstanding * as 'starts with'
Using character class [] without anchors
3. Given the collection documents: [{"city": "London"}, {"city": "london"}, {"city": "Londonderry"}], what will the query {"city": {"$regex": "^London$", "$options": "i"}} return?
medium
A. []
B. [{"city": "London"}]
C. [{"city": "Londonderry"}]
D. [{"city": "London"}, {"city": "london"}]
Solution
Step 1: Analyze the regex pattern and options
The pattern ^London$ matches exactly 'London' from start to end. The option i makes it case-insensitive.
Step 2: Check which documents match
"London" matches exactly, "london" matches ignoring case, but "Londonderry" does not match because it has extra characters.
Final Answer:
[{"city": "London"}, {"city": "london"}] -> Option D
Quick Check:
Exact match ignoring case = [{"city": "London"}, {"city": "london"}] [OK]
Hint: Use ^ and $ for exact match; 'i' option ignores case [OK]
Common Mistakes:
Ignoring case option and missing lowercase match
Assuming partial matches without anchors
Confusing $regex with substring search
4. Identify the error in this MongoDB query to find documents where title contains 'book' case-insensitive: {"title": {"$regex": "/book/i"}}
medium
A. The $regex operator cannot be used with strings.
B. The regex pattern should not be enclosed in slashes.
C. The $options field is missing.
D. The field name should be $pattern, not $regex.
Solution
Step 1: Understand MongoDB regex syntax
In MongoDB, the regex pattern is a string without enclosing slashes. Options like 'i' go in a separate $options field.
Step 2: Identify the mistake in the query
The pattern "/book/i" incorrectly includes slashes and option inside the string. It should be {"$regex": "book", "$options": "i"}.
Final Answer:
The regex pattern should not be enclosed in slashes. -> Option B
Quick Check:
MongoDB regex no slashes, use $options [OK]
Hint: Do not use /slashes/ in MongoDB regex patterns [OK]
Common Mistakes:
Including slashes around regex pattern
Putting options inside the pattern string
Using wrong field names like $pattern
5. You want to find all documents where the description field contains either 'cat' or 'dog' (case-insensitive). Which MongoDB query correctly uses regex to achieve this?
hard
A. {"description": {"$regex": "cat|dog", "$options": "i"}}
B. {"description": {"$regex": "[cat|dog]", "$options": "i"}}
C. {"description": {"$regex": "(cat|dog)", "$options": "g"}}
D. {"description": {"$regex": "cat|dog"}}
Solution
Step 1: Understand regex alternation and options
The pattern cat|dog matches either 'cat' or 'dog'. The i option makes it case-insensitive.
Step 2: Evaluate each option
{"description": {"$regex": "cat|dog", "$options": "i"}} correctly uses the pattern and case-insensitive option. {"description": {"$regex": "[cat|dog]", "$options": "i"}} uses character class incorrectly, matching any single character 'c', 'a', 't', '|', 'd', 'o', or 'g'. {"description": {"$regex": "(cat|dog)", "$options": "g"}} uses g which is not a valid MongoDB regex option. {"description": {"$regex": "cat|dog"}} misses the case-insensitive option.
Final Answer:
{"description": {"$regex": "cat|dog", "$options": "i"}} -> Option A
Quick Check:
Use | for OR and 'i' for ignore case [OK]
Hint: Use | inside regex and $options: 'i' for case-insensitive [OK]
Common Mistakes:
Using character classes [] instead of alternation |
Using invalid regex options like 'g' in MongoDB
Forgetting $options for case-insensitive search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.