MongoDB Academy · บทเรียน

ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search

ผู้เรียนจะระบุข้อจำกัดของดัชนีข้อความในตัว ได้แก่ มีได้หนึ่งดัชนีต่อคอลเลกชันและการรองรับภาษา และตัดสินใจว่าเมื่อใด Atlas Search จึงเป็นตัวเลือกที่ดีกว่า

บทเรียน 4 จาก 413 ขั้นตอน

ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Text Index Limitations Overview

MongoDB's built-in text index is a great starting point for full-text search, but it has several important limitations that become significant as your search requirements grow. Understanding these constraints helps you decide early whether to invest in the native text index or migrate to a dedicated search engine like Atlas Search.

One Text Index Per Collection

The most impactful limitation is that each collection can have only one text index. This means you cannot have separate text indexes for different search scenarios on the same collection—everything must be combined into a single index definition. If your search requirements change and you need to add or remove fields from the index, you must drop and rebuild the entire text index, which can be time-consuming on large collections.

// Can't have two text indexes on the same collection
db.articles.createIndex({ title: 'text' });  // OK
// db.articles.createIndex({ body: 'text' });  // ERROR!

// Must combine everything into one:
db.articles.dropIndex('title_text');  // rebuild required
db.articles.createIndex({ title: 'text', body: 'text' });

No Autocomplete Support

Native text indexes have no autocomplete capability. They match whole words (after stemming) but cannot suggest completions for partial input. For example, searching for 'mongo' will NOT match documents containing 'mongodb' because 'mongo' and 'mongodb' stem to different tokens. Implementing autocomplete with native text indexes requires complex workarounds like storing n-grams, which is inefficient.

// Native text index: partial word does NOT match
db.articles.find({ $text: { $search: 'mongo' } });
// Will NOT return docs with 'mongodb' because
// 'mongo' stems to 'mongo', 'mongodb' stems to 'mongodb'
// These are different index tokens

// Atlas Search: autocomplete field supports partial matching
// { $search: { autocomplete: { query: 'mongo', path: 'title' } } }

No Fuzzy Matching

Native text indexes have no fuzzy (approximate) matching. A typo like 'mongodab' will not match 'mongodb'—the token must be close enough to stem to the same root. For user-facing search boxes where typos are common, this results in frustrating zero-result searches. Atlas Search provides a fuzzy option with configurable edit distance to handle typos gracefully.

// Native $text: typo returns NO results
db.articles.find({ $text: { $search: 'mongodab' } }); // 0 results

// Atlas Search fuzzy option handles typos
// db.articles.aggregate([{
//   $search: {
//     text: {
//       query: 'mongodab',
//       path: 'title',
//       fuzzy: { maxEdits: 2 }  // tolerates up to 2 character edits
//     }
//   }
// }]);

Limited Language Support

Native text indexes support around 15 languages for stemming and stop words. While this covers most Western European languages, it has no support for Chinese, Japanese, Korean, Arabic, and many other languages that require word-boundary detection before tokenisation. Atlas Search uses Lucene analyzers that support a much broader set of languages and script-specific tokenisation rules.

// Supported natively: english, french, german, spanish, portuguese,
// italian, dutch, danish, norwegian, swedish, finnish,
// romanian, turkish, russian (with caveats)

// NOT supported natively:
// Chinese (no word boundaries), Japanese, Korean, Arabic
// Must use Atlas Search or an external search engine for these
db.articles.createIndex({ body: 'text' }, { default_language: 'english' });

No Synonym Support

Native text indexes cannot expand search terms to include synonyms. If a user searches for 'automobile', documents containing only 'car' will not match. Atlas Search supports synonym mappings that you configure in the index definition, enabling rich synonym expansion without application-level query rewriting.

// Native text: no synonym expansion
// 'car' search does NOT match 'automobile' documents
db.articles.find({ $text: { $search: 'car' } });

// Workaround: client-side synonym expansion (brittle)
const synonyms = { car: ['automobile', 'vehicle', 'auto'] };
const expandedSearch = [search, ...synonyms[search]].join(' ');
db.articles.find({ $text: { $search: expandedSearch } });

// Atlas Search: configure synonyms in the index definition

Performance at Scale

Native text indexes store all tokens in a single WiredTiger B-tree within the same storage engine as your operational data. For large corpora (tens of millions of documents with long text fields), this can cause write amplification and cache pressure that degrades overall database performance. Dedicated search engines like Lucene (which powers Atlas Search) are architecturally optimised for large token stores using inverted indexes with compression.

// Monitor text index size vs other indexes
const stats = db.articles.stats();
console.log('Index sizes:', stats.indexSizes);
// If text index is 10x larger than next biggest index,
// consider Atlas Search to offload the storage overhead

What Is Atlas Search?

Atlas Search is a fully managed, Apache Lucene-based search engine built into MongoDB Atlas. It runs as a separate service within your Atlas cluster and replicates data from your collections automatically. Atlas Search uses the aggregation $search stage rather than $text, and it provides autocomplete, fuzzy matching, facets, synonyms, custom scoring, and deep language support—all with the same MongoDB connection string.

// Atlas Search uses $search aggregation stage
db.articles.aggregate([
  { $search: {
    text: {
      query: 'mongodb tutorial',
      path: ['title', 'body'],
      fuzzy: { maxEdits: 1 }
    }
  }},
  { $limit: 10 },
  { $project: { title: 1, score: { $meta: 'searchScore' } } }
]);

Decision Matrix: Text Index vs Atlas Search

Use the native text index when: your collection has fewer than 1 million documents; your users search in one Western European language; you need basic keyword search with no typos or autocomplete; you want zero additional infrastructure cost. Use Atlas Search when: you need autocomplete or fuzzy search; your user base searches in multiple or non-Latin-script languages; you need faceted navigation; or your text search is central to the product and must scale.

// Native text: simple, zero extra cost, one language
// Good for: admin search, internal tools, small product
db.products.createIndex({ name: 'text', description: 'text' });

// Atlas Search: richer, requires Atlas M10+ cluster
// Good for: customer-facing search, multilingual, autocomplete
// Create via Atlas UI or API, then use $search in aggregation

Migrating From Text Index to Atlas Search

Migrating from native text to Atlas Search does not require changing your data model—Atlas Search indexes the same collection. The migration steps are: 1) create an Atlas Search index on the collection via the Atlas UI or API; 2) rewrite your find({ $text: ... }) queries to use the $search aggregation stage; 3) test and validate results; 4) drop the old text index to free up storage. Your documents don't move; only the index and query syntax change.

// BEFORE (native text)
db.articles.find(
  { $text: { $search: 'mongodb' } },
  { score: { $meta: 'textScore' } }
).sort({ score: { $meta: 'textScore' } });

// AFTER (Atlas Search - same result set, richer options)
db.articles.aggregate([
  { $search: { text: { query: 'mongodb', path: ['title', 'body'] } } },
  { $addFields: { score: { $meta: 'searchScore' } } },
  { $sort: { score: -1 } }
]);

When Neither Is Enough

For very demanding search requirements—custom ML-based ranking, vector similarity search (semantic search), multi-tenancy at scale, or full observability into query plans—even Atlas Search may not suffice. In those cases, dedicated search platforms like Elasticsearch, Solr, or Typesense are used alongside MongoDB, with a synchronisation layer (change streams or ETL) keeping the search index in sync with the MongoDB source of truth.

// Architecture pattern: MongoDB + external search engine
// 1. Write data to MongoDB (source of truth)
// 2. Change stream replicates new/updated docs to Elasticsearch
// 3. Search queries go to Elasticsearch
// 4. Read queries for known IDs go directly to MongoDB

// MongoDB change stream listener (Node.js)
const changeStream = db.articles.watch();
for await (const change of changeStream) {
  await elasticsearchClient.index({ id: change.documentKey._id, ...change.fullDocument });
}

Quick Check

Test your understanding of text index limitations and Atlas Search.

Lesson Recap

In this lesson you learned: native text indexes have key limitations including one-per-collection, no autocomplete, no fuzzy matching, and limited language support, Atlas Search (Lucene-based) solves these with autocomplete, fuzzy, synonyms, and broad language coverage, and the right choice depends on scale and feature requirements. Next up we explore array operators and queries.

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search”

ผู้เรียนจะระบุข้อจำกัดของดัชนีข้อความในตัว ได้แก่ มีได้หนึ่งดัชนีต่อคอลเลกชันและการรองรับภาษา และตัดสินใจว่าเมื่อใด Atlas Search จึงเป็นตัวเลือกที่ดีกว่า คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม

ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างดัชนีข้อความบนฟิลด์สตริง
  2. การเรียกใช้การค้นหา $text ด้วยวลีและการปฏิเสธ
  3. การเรียงลำดับตามคะแนนข้อความด้วย $meta
  4. ข้อจำกัดของดัชนีข้อความและเวลาที่ควรใช้ Atlas Search
← กลับไปที่ MongoDB Academy