MongoDB Text Search
Powerful full-text search capabilities in MongoDB
🔍 What is Text Search?
MongoDB Text Search allows you to search string content in your collections efficiently. It supports language-specific stemming, stop words, and relevance scoring for powerful search functionality.
// Simple text search example
db.articles.find({ $text: { $search: "mongodb tutorial" } })
Result:
Returns all documents containing "mongodb" or "tutorial"
Key Text Search Features
Text Indexes
Create indexes for text search
db.articles.createIndex({ title: "text", content: "text" })
Phrase Search
Search for exact phrases
db.articles.find({ $text: { $search: "\"exact phrase\"" } })
Relevance Score
Sort by search relevance
db.articles.find(
{ $text: { $search: "mongodb" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } })
Exclude Terms
Exclude specific words
db.articles.find({ $text: { $search: "mongodb -sql" } })
🔹 Creating a Text Index
Text indexes enable full-text search on string fields. You can create single or compound text indexes to search across multiple fields efficiently.
// Create a text index on a single field
db.products.createIndex({ description: "text" })
// Create a compound text index on multiple fields
db.blog.createIndex({
title: "text",
content: "text",
tags: "text"
})
// Create with weights (importance)
db.articles.createIndex(
{ title: "text", content: "text" },
{ weights: { title: 10, content: 5 } }
)
Result:
Index created successfully. Title matches are 2x more important than content.
🔹 Basic Text Search Queries
Perform text searches using the $text operator. MongoDB searches for any of the specified terms by default and ranks results by relevance.
// Search for any term
db.articles.find({ $text: { $search: "mongodb database" } })
// Search for exact phrase
db.articles.find({ $text: { $search: "\"NoSQL database\"" } })
// Exclude terms with minus sign
db.articles.find({ $text: { $search: "database -sql" } })
// Case-insensitive search
db.articles.find({
$text: {
$search: "MongoDB",
$caseSensitive: false
}
})
Result:
Returns matching documents based on search criteria
🔹 Text Search with Scoring
MongoDB assigns a relevance score to each matching document. Use this score to sort results by relevance and show the most relevant matches first.
// Get text score and sort by relevance
db.articles.find(
{ $text: { $search: "mongodb tutorial" } },
{ score: { $meta: "textScore" }, title: 1 }
).sort({ score: { $meta: "textScore" } })
// Filter by minimum score
db.articles.find(
{
$text: { $search: "mongodb" },
score: { $meta: "textScore" }
}
).filter({ score: { $gte: 1.5 } })
Result:
Documents sorted by relevance score (highest first)
🔹 Language-Specific Search
MongoDB supports text search in multiple languages with language-specific stemming and stop words for more accurate search results in different languages.
// Create index with default language
db.articles.createIndex(
{ content: "text" },
{ default_language: "english" }
)
// Search with specific language
db.articles.find({
$text: {
$search: "base de datos",
$language: "spanish"
}
})
// Multiple language support
db.articles.createIndex(
{ title: "text", content: "text" },
{ default_language: "english" }
)
// Document-level language override
db.articles.insertOne({
title: "Artículo en español",
content: "Contenido...",
language: "spanish"
})
Supported Languages:
English, Spanish, French, German, Portuguese, Italian, Russian, and 20+ more
🔹 Practical Example
Complete example of implementing text search in a blog application with index creation, insertion, and various search queries.
// 1. Create collection and text index
db.blog.createIndex(
{ title: "text", content: "text", tags: "text" },
{ weights: { title: 10, tags: 5, content: 1 } }
)
// 2. Insert sample documents
db.blog.insertMany([
{
title: "Getting Started with MongoDB",
content: "MongoDB is a NoSQL database...",
tags: ["mongodb", "database", "tutorial"]
},
{
title: "Advanced MongoDB Queries",
content: "Learn complex aggregation...",
tags: ["mongodb", "advanced", "queries"]
}
])
// 3. Search and display results
db.blog.find(
{ $text: { $search: "mongodb tutorial" } },
{ score: { $meta: "textScore" }, title: 1 }
).sort({ score: { $meta: "textScore" } }).limit(10)
Output:
Top 10 most relevant blog posts sorted by score