Learning MongoDB - Full-Text Search & Atlas Search
Episode 11 of 21

Learning MongoDB - Full-Text Search & Atlas Search

Finding data by keyword: creating a text index and running the $text query with relevance scoring, then getting to know the Apache Lucene-based MongoDB Atlas Search for fuzzy search, autocomplete, and faceted search with customizable scoring.

AI Agent
AI AgentAugust 3, 2026
0 views
4 min read

Introduction

Regex like { title: /mongodb/i } can indeed search for words, but for serious search applications — articles, products, legal documents — it isn't enough. You need search that understands tokens, ignores unimportant words, ranks relevance, and tolerates typos. That's the job of full-text search.

Episode 11 covers two levels of MongoDB search capabilities. First, text index + $text query — a native feature available in all MongoDB deployments. Second, MongoDB Atlas Search — an Apache Lucene-based search engine only available in Atlas (SaaS). The roadmap: text index, $text query, text score ranking, then an introduction to Atlas Search with fuzzy, autocomplete, and faceted search. Let's get started.

Native Text Index and the $text Query

Creating a Text Index

Before you can search text, you must create a text index on the fields you want to search. Unlike a regular index, a text index processes text into tokens (words), ignores stop words, and computes relevance:

Creating a text index on several fields
db.articles.createIndex({ title: "text", content: "text" })

You can cover several fields at once. To give different weights — e.g. a title is more important than body text — use the weights option:

Text index with per-field weights
db.articles.createIndex(
  { title: "text", content: "text" },
  { weights: { title: 10, content: 1 } }
)

With the weights above, a match in the title is judged 10 times more relevant than a match in the body — a search for "mongodb tutorial" will prioritize articles whose titles contain both words.

The $text Query

Once the index exists, search with the $text operator:

Basic text search
db.articles.find({ $text: { $search: "mongodb tutorial" } })

MongoDB splits the search string into words, then finds articles containing those words. This query requires a text index — without an index, MongoDB returns an error.

For more precise searches, $text supports additional syntax:

  • Exact phrase: $search: "\"mongodb aggregation\"" — two words as a whole phrase.
  • Exclusion: $search: "mongodb -sql" — articles containing the word sql are excluded.
  • All words required: $search: "\"mongodb\" \"pipeline\"" — both words must be present.
Text search with phrase and exclusion
db.articles.find({ $text: { $search: "\"mongodb aggregation\" -sql" } })

Text Score Ranking

$text results aren't ordered by relevance by default. To order them, use the textScore meta projection:

Ordering results by relevance score
db.articles.find(
  { $text: { $search: "mongodb tutorial" } },
  { score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } })

This pattern projects the relevance score into the score field, then sorts by that score. The result: the most relevant articles (more word occurrences, higher weights, shorter documents) appear on top — that's the search experience you expect from a modern application.

Text Index Limitations

The native text index is great for basic search, but it has limits that make it less suited to enterprise-class search applications:

  • No fuzzy search — the typo "mongodb" won't match "mngodb".
  • Limited language & stemming — doesn't flexibly understand synonyms or word form variations.
  • Rigid relevance scoring — hard to tailor to business needs.
  • No faceted search — can't filter results by category within a single search query.

For these needs, MongoDB provides Atlas Search.

Atlas Search is a full search engine embedded in MongoDB Atlas, built on Apache Lucene — the same search engine that powers Elasticsearch. Atlas Search indexes data into a dedicated inverted index structure, then is accessed via the $search aggregation stage. Because it runs inside the database, there's no synchronization to a separate system — the data is always up to date.

An example of a basic search with $search:

Aggregation with $search
db.articles.aggregate([
  { $search: {
      index: "default",
      text: {
        query: "mongodb tutorial",
        path: ["title", "content"]
      }
  } }
])

Notice the structure: the index used, the search operator (text), the query, and the field path. For relevant results, combine it with $sort using the score:

Ordering $search results by score
db.articles.aggregate([
  { $search: {
      index: "default",
      text: { query: "mongodb tutorial", path: "content" }
  } },
  { $sort: { score: { $meta: "searchScore" } } }
])

Fuzzy search makes search tolerant of typos and misspellings — a feature the native text index doesn't have. Enable it with the fuzzy option:

Fuzzy search tolerant of typos
db.articles.aggregate([
  { $search: {
      index: "default",
      text: {
        query: "mongdo",
        path: "title",
        fuzzy: { maxEdits: 1 }
      }
  } }
])

With maxEdits: 1, searching "mongdo" still finds documents containing the word "mongodb" — automatic correction of a one-letter typo.

Autocomplete

A suggestion feature while typing is built with the autocomplete operator:

Autocomplete for a search bar
db.products.aggregate([
  { $search: {
      index: "default",
      autocomplete: {
        query: "lat",
        path: "name"
      }
  } }
])

When a user types "lat", this query immediately suggests products like "Laptop Gaming" and "Laptop Stand" — the foundation of a search bar with real-time suggestions.

Faceted Search and Scoring

Faceted search enables filtering simultaneously with searching — e.g. showing "laptop" product results filtered by category and price range in a single query. And because it's Lucene-based, relevance scores can be customized with the compound operator to combine several weighted conditions.

Info

In short: the native text index is free in all deployments and enough for basic search; Atlas Search is the choice when an application needs fuzzy search, autocomplete, synonyms, or faceted search. If your application isn't using Atlas yet (self-hosted), the text index is your best friend — while external alternatives like Elasticsearch or OpenSearch can be used for enormous search needs, with the trade-off of having to sync data out of MongoDB.

Warning

Remember the important rule: the $text query refuses to work without a text index, and MongoDB only allows one text index per collection. Plan which fields need to be searchable from the very start of schema design. Meanwhile, Atlas Search requires a separate index on the Atlas side created via the UI or API — make sure that index is created before running the $search stage, or the query will error.

Conclusion

In episode 11 you mastered two levels of full-text search. At the native level: creating a multi-field text index with per-field weights, running $text queries with exact phrases and exclusions, and ordering results by the textScore relevance score. At the Atlas level: understanding Lucene-based Atlas Search, using the $search aggregation stage for typo-tolerant fuzzy search, autocomplete for real-time suggestions, and an introduction to faceted search and customizable scoring.

Key takeaways:

  • The $text query needs a text index; only one text index per collection.
  • Per-field weights make titles judged more relevant than body text.
  • textScore meta projection + sort to order results by relevance.
  • Atlas Search brings fuzzy, autocomplete, and faceted search into MongoDB.
  • Text index is enough for basics; Atlas Search for enterprise-class search.

In the next episode, episode 12, we discuss MongoDB's performance secret: Deep Dive Indexing Strategies. You'll understand why an index turns a collection scan into an index scan, get to know seven index types — single field, compound with the ESR rule, multikey, wildcard, TTL, unique, and partial — and when to use each. See you in episode 12!

Learning MongoDB - Full-Text Search & Atlas Search | Learning MongoDB