A text index in MongoDB allows you to perform efficient text search on string content within documents. It indexes the text fields to quickly find documents matching search terms.
Click to reveal answer
beginner
How do you create a text index on a field named 'description' in MongoDB?
Use the command: db.collection.createIndex({ description: "text" }) This creates a text index on the 'description' field for text search.
Click to reveal answer
beginner
What operator is used to perform a text search query in MongoDB?
The $text operator is used in the query to search text indexed fields. For example: { $text: { $search: "keyword" } }
Click to reveal answer
intermediate
Can you create a text index on multiple fields in MongoDB? How?
Yes, you can create a text index on multiple fields by specifying them with the value "text" in the index. For example: db.collection.createIndex({ title: "text", description: "text" })
Click to reveal answer
intermediate
What is the effect of the $language option in a text search query?
The $language option sets the language for the search to apply language-specific rules like stemming and stop words. For example, { $text: { $search: "run", $language: "english" } }.
Click to reveal answer
Which MongoDB command creates a text index on the 'content' field?
Adb.collection.createIndex({ content: 1 })
Bdb.collection.createTextIndex({ content: 1 })
Cdb.collection.createIndex({ content: "text" })
Ddb.collection.createText({ content: "text" })
✗ Incorrect
The correct syntax to create a text index is using createIndex with the field set to "text".
What operator do you use to search text indexed fields in MongoDB?
A$search
B$indexText
C$findText
D$text
✗ Incorrect
The $text operator is used in queries to perform text search on text indexed fields.
Can a MongoDB collection have only one text index?
AYes, only one text index per collection is allowed
BNo, multiple text indexes are allowed
CYes, but only if fields are different
DNo, text indexes are unlimited
✗ Incorrect
MongoDB allows only one text index per collection, but it can cover multiple fields.
What does the $language option do in a text search query?
ASets the language for stemming and stop words in search
BSpecifies the language for sorting results
CFilters documents by language field
DChanges the database language
✗ Incorrect
The $language option applies language-specific rules like stemming and stop words during text search.
Which of these queries searches for documents containing the word 'apple' in text indexed fields?
A{ text: "apple" }
B{ $text: { $search: "apple" } }
C{ $search: "apple" }
D{ $find: "apple" }
✗ Incorrect
The correct query uses $text with $search to find documents matching the word 'apple'.
Explain how to create and use a text index in MongoDB for searching text fields.
Think about how to prepare the collection and then how to search.
You got /4 concepts.
Describe the role of the $language option in MongoDB text search and why it matters.
Consider how language rules affect word matching.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of creating a text index in MongoDB?
easy
A. To encrypt text data for security
B. To enable fast searching of text within string fields
C. To store large text files inside the database
D. To sort documents by text fields alphabetically
Solution
Step 1: Understand what a text index does
A text index allows MongoDB to quickly search text inside string fields of documents.
Step 2: Compare options with the purpose
Sorting, storing files, or encrypting are not functions of text indexes.
Final Answer:
To enable fast searching of text within string fields -> Option B
Quick Check:
Text index = fast text search [OK]
Hint: Text index = fast search inside text fields [OK]
Common Mistakes:
Confusing text index with sorting index
Thinking text index stores files
Assuming text index encrypts data
2. Which of the following is the correct syntax to create a text index on the description field in a MongoDB collection named products?
easy
A. db.products.createTextIndex({ description: 1 })
B. db.products.createIndex({ description: 'string' })
C. db.products.createIndex({ description: 'text' })
D. db.products.createIndex({ description: 1 })
Solution
Step 1: Recall the syntax for creating a text index
The correct method is createIndex with the field set to 'text' type.
Step 2: Check each option
db.products.createIndex({ description: 'text' }) uses createIndex with { description: 'text' }, which is correct. Others use wrong methods or types.
Final Answer:
db.products.createIndex({ description: 'text' }) -> Option C
Quick Check:
Use createIndex with 'text' type [OK]
Hint: Use createIndex({field: 'text'}) to make text index [OK]
Common Mistakes:
Using 'string' instead of 'text' for index type
Using createTextIndex (not a valid method)
Creating normal ascending index (1) instead of text
3. Given a collection articles with a text index on fields title and content, what will the following query return?
A. Documents containing both words 'mongodb' and 'tutorial' in title or content
B. Documents containing exactly the phrase 'mongodb tutorial' only
C. Documents containing either 'mongodb' or 'tutorial' in any field
D. Documents sorted alphabetically by title
Solution
Step 1: Understand $text and $search behavior
The $search operator looks for documents containing all search words (AND behavior) in indexed text fields.
Step 2: Analyze the query
The query searches for documents with both 'mongodb' and 'tutorial' in either title or content fields.
Final Answer:
Documents containing both words 'mongodb' and 'tutorial' in title or content -> Option A
Quick Check:
$search finds all words, not exact phrase [OK]
Hint: $search finds all words, not exact phrase [OK]
Common Mistakes:
Thinking $search matches exact phrase only
Assuming $search matches any single word (OR)
Expecting sorting by title automatically
4. You tried to create two text indexes on a collection: one on title and another on description. What is the problem with this approach?
medium
A. MongoDB allows only one text index per collection
B. Text indexes must be created on numeric fields only
C. You must create text indexes using createTextIndex method
D. Text indexes cannot be created on multiple fields
Solution
Step 1: Recall MongoDB text index limitation
MongoDB allows only one text index per collection, but it can cover multiple fields.
Step 2: Evaluate the options
MongoDB allows only one text index per collection correctly states the limitation. Others are incorrect about field types or methods.
Final Answer:
MongoDB allows only one text index per collection -> Option A
Quick Check:
One text index per collection rule [OK]
Hint: Only one text index allowed per collection [OK]
Common Mistakes:
Trying to create multiple text indexes on same collection
Using wrong method name for creating text index
Thinking text indexes only work on numbers
5. You want to create a text index on title and summary fields but give title twice the search weight of summary. Which is the correct way to create this weighted text index?