A. Documents containing either 'mongodb' or 'tutorial' in the content field
B. Documents containing both words 'mongodb' and 'tutorial' in the content field
C. Documents containing the exact phrase 'mongodb tutorial' only
D. Documents sorted alphabetically by content
Solution
Step 1: Understand $text and $search behavior
The $search operator looks for documents containing any of the words listed, not necessarily all or exact phrases.
Step 2: Analyze the options
Documents containing either 'mongodb' or 'tutorial' in the content field correctly states it returns documents containing either word. Documents containing both words 'mongodb' and 'tutorial' in the content field requires both words, which is not default. Documents containing the exact phrase 'mongodb tutorial' only requires exact phrase, which needs quotes. Documents sorted alphabetically by content is unrelated.
Final Answer:
Documents containing either 'mongodb' or 'tutorial' in the content field -> Option A
Quick Check:
$search finds any listed words by default [OK]
Hint: By default, $search matches any listed words [OK]
Common Mistakes:
Assuming $search matches all words by default
Thinking $search matches exact phrases without quotes
Confusing search results with sorting
4. You run this query but get an error:
db.posts.find({ $text: { $search: "database" } })
What is the most likely cause?
medium
A. The query syntax requires parentheses
B. The $search operator is misspelled
C. No text index exists on the collection
D. The field name 'database' is invalid
Solution
Step 1: Understand requirements for $text queries
MongoDB requires a text index on at least one field to use $text queries without error.
Step 2: Check the options for likely causes
No text index exists on the collection correctly identifies missing text index as the cause. Other options describe syntax or spelling errors which are not present.
Final Answer:
No text index exists on the collection -> Option C
Quick Check:
$text query needs text index [OK]
Hint: Ensure text index exists before using $text queries [OK]
Common Mistakes:
Ignoring the need for a text index
Thinking $search syntax is wrong
Assuming field names cause $text errors
5. You want to find documents that contain the exact phrase "fast search" in the summary field which has a text index. Which query will correctly do this?
hard
A. db.collection.find({ $text: { $search: '"fast search"' } })
B. db.collection.find({ $text: { $search: 'fast search' } })
C. db.collection.find({ summary: { $regex: 'fast search' } })
D. db.collection.find({ $text: { $search: '\"fast search\"' } })
Solution
Step 1: Recall how to search exact phrases with $text
Exact phrase search requires double quotes around the phrase inside the $search string.
Step 2: Evaluate each option
db.collection.find({ $text: { $search: '"fast search"' } }) correctly uses single quotes outside and double quotes inside to search exact phrase. db.collection.find({ $text: { $search: 'fast search' } }) searches words separately. db.collection.find({ summary: { $regex: 'fast search' } }) uses regex, not text search. db.collection.find({ $text: { $search: '\"fast search\"' } }) escapes quotes incorrectly.