A text index tokenizes string values into individual words (stems), strips stopwords (the, a, is...), and builds an inverted index enabling fast full-text search. Only one text index per collection is allowed, but it can span multiple fields.
// Create text index on a single field db.articles.createIndex({ title: "text" }) // Create text index spanning multiple fields db.articles.createIndex({ title: "text", body: "text", tags: "text" }) // Weighted text index — title matches count more than body db.articles.createIndex( { title: "text", body: "text", tags: "text" }, { weights: { title: 10, // title match worth 10x tags: 5, // tag match worth 5x body: 1 // body match worth 1x (default) }, name: "article_text_idx", default_language: "english" } ) // Wildcard text index — index ALL string fields in every document db.products.createIndex({ "$**": "text" }) // Useful when document structure is dynamic or fields are unknown // Downside: large index; may include fields you don't want searchable // Compound text index — text + additional field for filtering db.articles.createIndex({ body: "text", status: 1 }) // Enables: $text query + { status: "published" } filter using same index
A collection can have at most one text index — this includes wildcard text indexes. If you need text search on different subsets of fields with different configurations, you cannot use multiple text indexes. Use the wildcard text index or Atlas Search for more flexibility.