Text indexes for search in MongoDB - Time & Space Complexity
When using text indexes in MongoDB, it's important to understand how the search time changes as the amount of data grows.
We want to know how the search speed changes when we look for words in bigger collections.
Analyze the time complexity of the following code snippet.
// Create a text index on the 'description' field
db.products.createIndex({ description: "text" })
// Search for documents containing the word 'coffee'
db.products.find({ $text: { $search: "coffee" } })
This code creates a text index on a field and then searches for documents containing a specific word.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database looks up the search word in the text index structure.
- How many times: The lookup happens once per search query, but the index internally may check multiple entries depending on the word frequency.
As the number of documents grows, the index helps keep search fast by avoiding scanning every document.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few index lookups, very fast |
| 100 | More index entries checked, still quick |
| 1000 | More index entries, but search remains efficient |
Pattern observation: The search time grows slowly compared to the number of documents because the index narrows down the search quickly.
Time Complexity: O(log n + k)
This means the search time grows slowly as the collection grows, thanks to the index structure, plus a factor depending on the number of matching documents.
[X] Wrong: "Searching with a text index scans every document one by one."
[OK] Correct: The text index lets MongoDB jump directly to relevant documents without checking all of them, making search much faster.
Understanding how text indexes speed up search shows you know how databases handle big data efficiently, a useful skill for many projects.
"What if we searched without a text index? How would the time complexity change?"
Practice
text index in MongoDB?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 BQuick Check:
Text index = fast text search [OK]
- Confusing text index with sorting index
- Thinking text index stores files
- Assuming text index encrypts data
description field in a MongoDB collection named products?Solution
Step 1: Recall the syntax for creating a text index
The correct method iscreateIndexwith the field set to'text'type.Step 2: Check each option
db.products.createIndex({ description: 'text' }) usescreateIndexwith{ description: 'text' }, which is correct. Others use wrong methods or types.Final Answer:
db.products.createIndex({ description: 'text' }) -> Option CQuick Check:
Use createIndex with 'text' type [OK]
- Using 'string' instead of 'text' for index type
- Using createTextIndex (not a valid method)
- Creating normal ascending index (1) instead of text
articles with a text index on fields title and content, what will the following query return?db.articles.find({ $text: { $search: "mongodb tutorial" } })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 AQuick Check:
$search finds all words, not exact phrase [OK]
- Thinking $search matches exact phrase only
- Assuming $search matches any single word (OR)
- Expecting sorting by title automatically
title and another on description. What is the problem with this approach?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 AQuick Check:
One text index per collection rule [OK]
- Trying to create multiple text indexes on same collection
- Using wrong method name for creating text index
- Thinking text indexes only work on numbers
title and summary fields but give title twice the search weight of summary. Which is the correct way to create this weighted text index?Solution
Step 1: Recall syntax for weighted text indexes
Weights option is an object namedweightsspecifying field weights.Step 2: Check options for correct spelling and values
db.books.createIndex({ title: 'text', summary: 'text' }, { weights: { title: 2, summary: 1 } }) usesweightscorrectly and assigns title weight 2, summary 1 as required.Final Answer:
db.books.createIndex({ title: 'text', summary: 'text' }, { weights: { title: 2, summary: 1 } }) -> Option DQuick Check:
Use 'weights' option with correct field weights [OK]
- Using 'weight' instead of 'weights' option
- Swapping weights between fields
- Not specifying weights object correctly
Ready to prove it?
Pass the test and this pattern moves unknown
