Fixing the MongoDB Error: 'text index required for $text query'

intermediate🍃 MongoDB2026-07-15| MongoDB (All Versions), Node.js (Mongoose/MongoDB Driver), Python (PyMongo), MongoDB Shell

Error Message

MongoServerError: text index required for $text query
#mongodb#mongoose#database-indexing#full-text-search

The ProblemYou're trying to run a full-text search in MongoDB using the $text operator, but instead of results, you get a wall of red text. It usually looks like this:

MongoServerError: text index required for $text query

I ran into this recently while building a search bar for a documentation site. I wanted to find keywords across titles and body text using a query like this:

db.articles.find({ $text: { $search: "performance optimization" } })

It looks like it should work. However, MongoDB handles full-text searches differently than standard field lookups. While a query like { status: "active" } can run (albeit slowly) without an index, a $text query is physically unable to execute until you define a text index.

Why this happensUnder the hood, the $text operator does a lot of heavy lifting. It performs stemming (turning "running" into "run"), strips out stop words like "the" or "and," and tokenizes the string. To do this efficiently, MongoDB requires a specialized data structure. If you haven't told the database which fields to include in this structure, it has no map to follow. It simply gives up and throws the error.

The Step-by-Step Fix### 1. Check your existing indexesStart by seeing what indexes are already there. Run this in the MongoDB Shell (mongosh):

db.articles.getIndexes()

Look at the results. If you don't see a key with the value "text", that’s the missing piece. You might see _id_ or an index on category, but those won't help with a text search.

2. Create the Text IndexYou need to explicitly tell MongoDB which fields to index for search. If you want to search both the title and content fields, run this command:

db.articles.createIndex({ 
  title: "text", 
  content: "text" 
})

In MongoDB 4.2 and later, this happens in the background by default. It won't lock your database, but it will consume I/O. For a collection with 1 million small documents, this might take 30 to 60 seconds depending on your hardware.

3. The "Search Everything" WildcardIf you're prototyping and want to search every string field without listing them all, use the wildcard specifier:

db.collection.createIndex({ "$**": "text" })

Use this sparingly. A wildcard text index can easily increase your storage usage by 30% or more because it indexes every single string in the document.

Fixing it in Mongoose (Node.js)If you use Mongoose, don't just fix it in the shell. You need to update your Schema so the index persists when you deploy to production. Here is a clean way to set it up:

const articleSchema = new mongoose.Schema({
  title: String,
  content: String,
  author: String
});

// Define the text index at the schema level
articleSchema.index({ title: 'text', content: 'text' });

const Article = mongoose.model('Article', articleSchema);

A quick warning: Mongoose creates indexes when the app starts. If you already have a different index on that collection, Mongoose might fail to build the new one silently. If the error persists, drop the existing indexes in the shell using db.articles.dropIndexes() and restart your Node server.

The "One Index" RuleA common trap is trying to create multiple text indexes. MongoDB only allows one text index per collection. If you try to run these as two separate commands, the second one will fail:

db.posts.createIndex({ title: "text" })
db.posts.createIndex({ description: "text" }) // Error!

To search both, you must combine them into a single compound index as shown in Step 2.

How to verify the fixAfter creating the index, verify that your query is actually using it. Append .explain("executionStats") to your query:

db.articles.find({ $text: { $search: "database" } }).explain("executionStats")

In the output, look for the winningPlan. If you see a stage named TEXT_MATCH, you’re good to go. The error should be gone, and your search results should appear instantly.

Key Takeaways- Storage Costs: Text indexes are much larger than standard B-tree indexes. Expect a significant jump in RAM and disk usage.- Weights: You can make the title more important than the body by assigning weights (e.g., { title: "text", content: "text" }, { weights: { title: 10, content: 2 } }).- Language: MongoDB defaults to English. If your content is in French or Spanish, specify the default_language in the index options to ensure stemming works correctly.

Related Error Notes