codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

18 / 65 topics
12Querying Basics13Find Queries14Projection15Sorting and Limiting16Aggregation Framework17Operators in MongoDB18Text Search
Tutorials/MongoDB/Text Search
🍃MongoDB

Text Search

Updated 2026-04-20
3 min read

Text Search in MongoDB

Text search is a powerful feature that allows you to perform full-text searches on string content within your documents. This guide will walk you through the basics of setting up and using text search in MongoDB, including creating text indexes, performing text queries, and understanding best practices.

Introduction to Text Indexes

In MongoDB, text indexes support searching for strings in a collection. Unlike other types of indexes, text indexes are designed to handle full-text searches efficiently. They tokenize the indexed fields into words, remove stop words (common words like "the" or "and"), and store the tokens in a way that allows for efficient search operations.

Creating Text Indexes

To enable text search on a collection, you need to create a text index on one or more fields. Here's how you can create a text index:

db.collection.createIndex({ field1: "text", field2: "text" })

This command creates a text index on field1 and field2. You can include as many fields as needed in the index.

Example

Suppose you have a collection named products with documents like this:

{
  "_id": ObjectId("..."),
  "name": "Wireless Mouse",
  "description": "A high-performance wireless mouse for gaming and office use."
}

To create a text index on the name and description fields, you would run:

db.products.createIndex({ name: "text", description: "text" })

Performing Text Searches

Once you have created a text index, you can perform text searches using the $text operator. This operator allows you to search for words in the indexed fields.

Basic Text Search

To perform a basic text search, use the following syntax:

db.collection.find({ $text: { $search: "searchTerm" } })

For example, to find all products containing the word "mouse":

db.products.find({ $text: { $search: "mouse" } })

Case Insensitivity and Diacritic Insensitivity

Text searches in MongoDB are case insensitive and diacritic insensitive by default. This means that searching for "Mouse" or "møuse" will return the same results.

Phrase Searches

To search for an exact phrase, enclose the phrase in double quotes:

db.products.find({ $text: { $search: "\"wireless mouse\"" } })

This query will only return documents where the exact phrase "wireless mouse" appears.

Scoring and Sorting Results

MongoDB automatically assigns a score to each document that matches the text search criteria. The score is based on how well the document matches the search terms, with higher scores indicating better matches. You can sort the results by this score:

db.products.find({ $text: { $search: "mouse" } }).sort({ score: { $meta: "textScore" } })

This query sorts the results in descending order of relevance.

Language Support

MongoDB supports text search for multiple languages. You can specify the language when creating a text index to enable language-specific tokenization and stop words:

db.products.createIndex({ name: "text", description: "text" }, { default_language: "english" })

This example sets the default language to English, which affects how words are tokenized and which stop words are removed.

Best Practices

  1. Create Indexes on Relevant Fields: Only create text indexes on fields that you expect to search frequently.
  2. Monitor Index Size: Text indexes can grow large, especially if they index many documents or long strings. Monitor the size of your indexes and consider optimizing them if necessary.
  3. Use Language-Specific Indexes: If your application supports multiple languages, use language-specific text indexes to improve search accuracy.
  4. Avoid Over-Indexing: Creating too many text indexes can impact write performance. Balance the need for fast searches with the overhead of maintaining indexes.

Conclusion

Text search is a powerful feature in MongoDB that allows you to perform efficient full-text searches on your data. By creating text indexes and using the $text operator, you can quickly find documents containing specific words or phrases. Understanding how to create and manage text indexes, as well as best practices for optimizing performance, will help you make the most of this feature in your applications.

Remember to regularly review and optimize your text indexes to ensure they meet the needs of your application while maintaining optimal performance.


PreviousOperators in MongoDBNext Indexing Basics

Recommended Gear

Operators in MongoDBIndexing Basics