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

54 / 65 topics
52Real-Time Applications53Change Streams54Full-Text Search55Geospatial Data56Time-Series Data57MongoDB Operations Framework
Tutorials/MongoDB/Full-Text Search
🍃MongoDB

Full-Text Search

Updated 2026-04-20
3 min read

Full-Text Search

Full-text search is a powerful feature that allows you to search for text within documents stored in your database. In this section, we will explore how to implement full-text search in MongoDB using the text index type. This feature is particularly useful when dealing with large volumes of textual data and requires efficient querying capabilities.

Introduction

MongoDB provides a robust full-text search feature that supports searching across multiple fields within documents. The full-text search functionality is built on top of text indexes, which are designed to handle complex queries involving multiple words and phrases.

Key Concepts

  • Text Index: A special index type in MongoDB that supports full-text search.
  • Analyzer: A component responsible for tokenizing the text into searchable terms.
  • Language Support: MongoDB supports a variety of languages for text analysis, including English, Spanish, French, German, etc.
  • Weights: You can assign weights to different fields within a document to influence the relevance score of search results.

Setting Up Full-Text Search

Creating a Text Index

To enable full-text search on a collection, you need to create a text index. Here's how you can do it:

db.collection.createIndex(
  { "fieldName": "text" },
  {
    weights: { "fieldName": 10 }, // Optional: Assign weight to the field
    default_language: "english", // Optional: Specify the language
    name: "textIndexName" // Optional: Name for the index
  }
)

Example

Let's assume we have a collection named products with documents containing fields like name, description, and category. We want to create a text index on these fields:

db.products.createIndex(
  {
    name: "text",
    description: "text",
    category: "text"
  },
  {
    weights: { name: 10, description: 5, category: 2 }, // Higher weight for 'name'
    default_language: "english"
  }
)

Performing Full-Text Queries

Once the text index is created, you can perform full-text searches using the $text operator. Here are some examples of how to use it:

Basic Search

To search for documents containing a specific word or phrase:

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

Case Insensitivity and Diacritic Sensitivity

By default, MongoDB's full-text search is case-insensitive and diacritic-sensitive. You can modify this behavior using the $language option:

db.products.find(
  { $text: { $search: "café", $language: "fr" } }
)

Scoring

MongoDB assigns a relevance score to each document based on how well it matches the search query. You can sort the results by this score:

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

Filtering Results

You can combine full-text search with other query conditions to filter results:

db.products.find(
  {
    $text: { $search: "laptop" },
    category: "Electronics"
  }
)

Best Practices

  1. Index Management: Regularly monitor and manage your text indexes to ensure optimal performance.
  2. Language Selection: Choose the appropriate language for text analysis based on your data's language.
  3. Weight Assignment: Assign weights to fields based on their importance to improve search relevance.
  4. Query Optimization: Use query optimization techniques to reduce the number of documents scanned during full-text searches.

Advanced Features

Phrase Search

To perform a phrase search, enclose the phrase in double quotes:

db.products.find(
  { $text: { $search: "\"ultra high definition\"" } }
)

Negation

You can exclude certain words from your search results using the - operator:

db.products.find(
  { $text: { $search: "laptop -notebook" } }
)

Wildcard Search

MongoDB supports wildcard searches using the * character. However, this feature is limited to suffixes:

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

Conclusion

Full-text search in MongoDB is a powerful tool for handling complex text queries efficiently. By leveraging text indexes and the $text operator, you can build robust search capabilities into your applications. Remember to follow best practices for index management and query optimization to ensure optimal performance.

In the next section, we will explore other advanced topics in MongoDB, including aggregation pipelines and sharding. Stay tuned!


PreviousChange StreamsNext Geospatial Data

Recommended Gear

Change StreamsGeospatial Data