BackendMongoDBDatabaseBackendAPINoSQL

MongoDB Full-Text Search: Index, Query, and Rank Text Data Effectively

MongoDB's built-in text indexes enable full-text search without a separate search engine. Learn how to implement search features with relevance scoring and faceted filtering.

Abdur Razzak

Abdur Razzak

Full-Stack Web Developer

May 26, 2026 8 min read

Adding search functionality to an application is a common requirement that many developers immediately reach for Elasticsearch or Algolia to solve. While dedicated search engines offer advanced capabilities, MongoDB's built-in text search is a capable solution for many use cases that does not require a separate infrastructure component, separate data synchronization logic, or additional operational complexity. MongoDB text indexes tokenize and stem text content across specified string fields, enabling efficient full-text queries that match documents containing specified words or phrases. The results include a calculated relevance score based on term frequency and the importance of matched fields. For applications with modest search requirements, MongoDB text search provides a practical solution entirely within the existing database infrastructure. Understanding its capabilities and limitations helps you decide when it is sufficient and when a dedicated search engine is justified.

Creating Text Indexes

Create a text index on the fields you want to make searchable using Mongoose schema configuration or the MongoDB shell createIndex command. You can create a text index on a single string field, multiple specific string fields, or all string fields in a document using the wildcard dollar-sign double-star notation. When indexing multiple fields, assign different weights to reflect the relative importance of each field in relevance scoring. For example, a product catalog might assign a weight of ten to the product title field and a weight of two to the description field, so documents where the search term appears in the title are ranked higher than documents where it only appears in the description. MongoDB limits each collection to a single text index, so the index must include all searchable fields. Consider the collection size and index build time for large existing collections before adding text indexes.

Running Text Search Queries

Query a text-indexed collection using the dollar-sign text operator with a dollar-sign search value containing the search terms. MongoDB's text search handles stemming automatically, matching variants of words: searching for run matches running, ran, and runs. Phrases can be enclosed in escaped double quotes to require exact phrase matching. Prepending a minus sign to a term excludes documents containing that term. The dollar-sign meta textScore projection field retrieves the relevance score calculated for each result, and sorting by this meta score in descending order returns the most relevant results first. In Mongoose, use the Model.find method with the text filter and include the score projection using the select method with the JSON score dollar meta textScore syntax. Combine text search with additional filter conditions to implement faceted search that narrows results by category, price range, or other attributes while searching text content.

Language Support and Case Sensitivity

MongoDB text indexes support stemming and stop-word removal for specific languages. The default language is English, which applies the Snowball stemming algorithm and removes common stop words like the, a, and is that have no search value. Specify a different default language at index creation time for non-English content, choosing from the list of supported languages. For multilingual content collections, store the language of each document in a designated field and specify that field as the language override field in the index configuration. MongoDB will then apply the appropriate stemmer and stop-word list for each document based on its individual language value. Text indexes are always case-insensitive by default and perform diacritic-insensitive matching in MongoDB 3.4 and later, treating accented characters as equivalent to their unaccented base characters.

Limitations and When to Use Elasticsearch Instead

MongoDB text search has several important limitations that should inform the decision about whether it is sufficient for a given use case. It does not support partial word matching or prefix searches, so searching for prog does not match programming. It does not provide autocomplete or typeahead suggestions. It cannot search within arrays of strings or nested documents effectively in all cases. The relevance scoring algorithm is simpler than the TF-IDF and BM25 algorithms used by Elasticsearch, producing less nuanced ranking for complex queries. It does not support synonyms, custom analyzers, or field boosting at query time. For applications that require autocomplete, faceted navigation with document counts per facet, fuzzy matching that tolerates misspellings, or highly tuned relevance ranking, a dedicated search engine is the appropriate choice.

Atlas Search: Elasticsearch-Grade Search in MongoDB Atlas

MongoDB Atlas offers Atlas Search, which is powered by an embedded Apache Lucene engine within the MongoDB Atlas managed service. Atlas Search provides capabilities comparable to Elasticsearch without a separate infrastructure component: full Lucene query syntax, autocomplete with edge n-gram indexing, fuzzy matching with configurable edit distance, synonym support, custom analyzers and tokenizers, faceted search with bucket counts, compound queries with field boosting, and relevance score explanations. Atlas Search indexes are defined as JSON documents specifying field analyzers and index options, and queries use the dollar-sign search aggregation pipeline stage. If you are already using MongoDB Atlas for your database and need advanced search capabilities, Atlas Search provides the best developer experience because it eliminates the data synchronization problem of keeping a separate search index in sync with your database.

Hybrid Search: Combining Vector and Text Search

MongoDB Atlas Vector Search enables a fundamentally different kind of search that finds semantically similar content rather than documents containing specific keywords. By storing vector embeddings generated by machine learning models alongside document content, you can find documents that are topically related to a search query even when they use completely different words. Combining traditional text search with vector search in a single query produces hybrid search results that match both keyword relevance and semantic similarity. Generate embeddings using models from OpenAI, Cohere, or local open-source models, store them as array fields in your documents, and create a vector search index specifying the number of dimensions and similarity metric. Run hybrid queries that blend the text search score and the vector similarity score with configurable weights to produce results that match on both explicit keywords and broader semantic meaning.

Share this article

All posts
#MongoDB#Database#Backend#API#NoSQL
Abdur Razzak — Full Stack Web Developer
Available for projects

Need a React or Next.js Developer?