Problem Statement
What type of index is required to perform text search in MongoDB?
Explanation
A text index is required to perform full-text search in MongoDB. Text indexes tokenize and stem the text content, allowing you to search for words and phrases within string fields.
You create a text index using the text keyword instead of 1 or negative 1. MongoDB supports text search queries using the dollar text operator. Text indexes can be created on multiple fields, and you can have only one text index per collection, but it can include multiple fields.
Code Solution
SolutionRead Only
// Create text index
db.articles.createIndex({ title: "text", body: "text" })
// Perform text search
db.articles.find({
$text: { $search: "mongodb database" }
})
// Search with exact phrase
db.articles.find({
$text: { $search: "\"NoSQL database\"" }
})