$text

noun

say it: “DOL-er TEKST” /ˈdɑlər ˈtɛkst/

A MongoDB query operator that searches the words in text fields covered by a text index.

What does $text do in a MongoDB query?

$text searches a collection's text index for the words in $search, in any case and any form, so "pancake" finds "Pancakes". The collection needs a text index first, or the query stops with an error.

db.recipes.createIndex({ name: "text", description: "text" });
db.recipes.find({ $text: { $search: "pancake" } }, { _id: 0, name: 1 });
// [ { name: 'Fluffy Pancakes' } ]

In a sentence

  • “The search box sends its words to $text, which found 'Pancakes' for 'pancake'.”
  • “$text gave an error until the collection had a text index.”

Level: Journeyman · More MongoDB terms at this level

Updated