MongoDB provides text search capabilities to perform full-text search on string fields within documents. With the help of text indexes, MongoDB can efficiently find documents containing specific words or phrases in text fields. Text search is ideal for situations where you need to perform searches on large amounts of unstructured text, such as descriptions, comments, articles, or other documents containing freeform text.
A text index is an index on string fields that allows for full-text search functionality. You can create a text index on one or more fields, and MongoDB will automatically tokenize the text and provide features like stemming, case insensitivity, and support for wildcard searches.
To create a text index on one or more fields, you use the "text"
option when calling createIndex()
.
javascript
Copy code
db.collection.createIndex({ fieldName: "text" })
Let’s say you have a posts
collection, and you want to perform text searches on the title
and content
fields. You can create a text index on both fields:
javascript
Copy code
db.posts.createIndex({ title: "text", content: "text" })
This will allow MongoDB to perform full-text searches on both title
and content
fields.
Once a text index is created, you can use the $text
operator in the find()
method to search for specific words or phrases in text-indexed fields.
javascript
Copy code
db.collection.find({ $text: { $search: "searchTerm" } })
Let’s assume you want to search for documents containing the word “MongoDB” in the title
or content
fields of the posts
collection:
javascript
Copy code
db.posts.find({ $text: { $search: "MongoDB" } })
This query will return all posts where either the title
or content
contains the word “MongoDB”.
You can search for exact phrases by enclosing the phrase in double quotes. MongoDB will search for documents containing that exact phrase.
javascript
Copy code
db.collection.find({ $text: { $search: "\"exact phrase\"" } })
Search for posts that contain the exact phrase “MongoDB tutorial”:
javascript
Copy code
db.posts.find({ $text: { $search: "\"MongoDB tutorial\"" } })
This will only return documents that contain the exact phrase “MongoDB tutorial” in either the title
or content
fields.
MongoDB supports basic boolean logic in text searches, including the AND and OR operators.
"MongoDB tutorial"
will return documents that contain both “MongoDB” and “tutorial”."$search"
operator to include multiple words or phrases and find documents that match any of them. Use a space between terms to indicate OR logic.To search for documents that contain both “MongoDB” and “tutorial”:
javascript
Copy code
db.posts.find({ $text: { $search: "MongoDB tutorial" } })
This will return all documents containing both “MongoDB” and “tutorial”.
You can also search for documents that contain either of two words:
javascript
Copy code
db.posts.find({ $text: { $search: "MongoDB OR tutorial" } })
This will return documents that contain either “MongoDB” or “tutorial”, or both.
MongoDB allows excluding words from the search results using a minus sign (-
). If you place a minus sign before a word, MongoDB will exclude documents that contain that word.
javascript
Copy code
db.collection.find({ $text: { $search: "term1 -term2" } })
To search for documents that contain the word “MongoDB” but not “tutorial”:
javascript
Copy code
db.posts.find({ $text: { $search: "MongoDB -tutorial" } })
This will return all documents that contain the word “MongoDB” but exclude those that also contain the word “tutorial”.
MongoDB assigns a relevance score to each document based on how well it matches the search terms. You can retrieve these scores to rank search results.
To include the relevance score in the query results, you can use the score
in the projection.
javascript
Copy code
db.collection.find( { $text: { $search: "searchTerm" } }, { score: { $meta: "textScore" } } )
To search for “MongoDB” and include the relevance score in the result:
javascript
Copy code
db.posts.find( { $text: { $search: "MongoDB" } }, { score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } })
This query returns documents containing “MongoDB” and sorts them by relevance, with documents that match the search term more closely receiving a higher score.
MongoDB’s text search is language-aware, meaning that it uses language-specific rules for stemming (reducing words to their base form) and stop words (common words like “the”, “and”, etc., that are ignored during the search).
You can specify the language for a text index, or let MongoDB default to a language (usually english
).
javascript
Copy code
db.collection.createIndex({ fieldName: "text" }, { default_language: "english" })
Creating a text index for a collection with a specific language:
javascript
Copy code
db.posts.createIndex({ title: "text", content: "text" }, { default_language: "english" })
This index will apply English-specific rules for stemming and stop word removal.
*
, ?
) or regular expressions for matching text in the way you might find in some search engines.You can also use text search in MongoDB’s aggregation framework to perform more advanced queries, like filtering, sorting, and transforming data based on text search results.
To perform a text search within an aggregation pipeline and include the score, use the $match
and $project
stages:
javascript
Copy code
db.posts.aggregate([ { $match: { $text: { $search: "MongoDB" } } }, { $project: { title: 1, score: { $meta: "textScore" } } }, { $sort: { score: { $meta: "textScore" } } } ])
This aggregation pipeline:
title
field and includes the score
.For more advanced full-text search features, such as phrase matching, wildcard queries, and more complex analyzers (including support for multiple languages and custom analyzers), MongoDB Atlas offers a powerful Atlas Full-Text Search feature built on Lucene.
You can create and manage Atlas Search Indexes directly from the MongoDB Atlas UI.