liveBook · Manning

Full-text search index

As explained in MongoDB 8.0 in Action, Third Edition

A text index is an index that supports text-search queries over string fields.

A MongoDB text index supports keyword searches across string fields. It can index one or multiple fields, but each collection can have only one text index. MongoDB’s Atlas Search provides broader full-text search capabilities and is recommended over traditional text indexes when partial-value searches are required. section 7.2.4

Use a text index when an application needs:

Index movie titles and plots

Create one text index covering both fields, then search for matching terms and return the highest-scoring results first: db.movies.createIndex({ title: "text", fullplot: "text" }) db.movies.find( { $text: { $search: "Zone drinks" } }, { score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } }).limit(3) A search for “Zone drinks” can return the movie “Friend Zone” with a text relevance score. Prefixing a term with a minus sign excludes it, as in $search: "-Zone drinks". section 7.2.4

Listing 7.9 Text index prototype
db.<collection>.createIndex(
   {
      <field1>: "text",
      <field2>: "text",
      ...
   }
)

Use the prototype to adapt a text index to one or more string fields. section 7.2.4

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains creation, query syntax, scoring, language behavior, and limitations without detailing index internals.
Examples
Uses movie titles and plots, the terms “Zone drinks,” relevance scores, and exclusion with “-Zone drinks.”
What it assumes
Assumes familiarity with MongoDB collections and queries while introducing text-index creation and the $text operator.
What you can do after
Apply it via mongosh by creating a multi-field text index and sorting $text results with textScore.
How it connects
Connects text indexes with $text queries, regular expressions, text scoring, and Atlas Search.

Also explained in

Compare how 3 books explain full-text search index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.