QA
Beginner LevelMongoDB & Mongoose

How do MongoDB Text Indexes ($text and $search) execute text queries?

❓ Interview Prompt: Explain Text Indexes, string tokenization, stemming, stop-word removal, $text query operator, and text score ranking ($meta).

MongoDB Text Indexes enable search capabilities across string fields inside collections:

Features:

•
Tokenizes words, converts to lowercase, and strips common stop-words ('the', 'an', 'is').
•
Stems words (e.g. 'running' $ ightarrow$ 'run').
•
Orders results using relevance score ranking via '{ score: { $meta: "textScore" } }'.

Code Implementation Example

typescript
import { ArticleModel } from './models/Article';

async function searchArticles(searchTerm: string) {
  // Requires text index on title/content fields
  const results = await ArticleModel.find(
    { $text: { $search: searchTerm } },
    { score: { $meta: 'textScore' } }
  ).sort({ score: { $meta: 'textScore' } });

  return results;
}
💡Executing full-text search with relevance score ranking in MongoDB.
🎯 Interview Key Takeaway

Create text indexes to enable word stemming and relevance score sorting for full-text collection queries.

#Text Index#$text#Search#Beginner
Back to all MongoDB & Mongoose Questions→

More MongoDB & Mongoose Interview Questions

Beginner LevelMongoDB & Mongoose

How does the MongoDB Aggregation Pipeline work? Explain $match, $group, $lookup, and $project.

Beginner LevelMongoDB & Mongoose

What is MongoDB and how does the BSON document data model work?

Beginner LevelMongoDB & Mongoose

What is the structure of a MongoDB ObjectId and how is it generated?