← Back to MongoDB Course | Chapter 8: Indexes | Lesson 4 of 7

Text Indexes क्या हैं

एक text index string fields को $text queries से searchable बनाता है।

In this page:

  1. Text Indexes
Syntax
javascript
db.collection_name.createIndex({ field1: "text", field2: "text" })

Text Indexes

createIndex({ title: "text", body: "text" }) stemming और language rules से words index करता है। Weights कुछ fields को ज़्यादा count कराते हैं, और default_language language set करता है।

प्रति collection सिर्फ एक text index allowed है, और queries $text को $search के साथ इस्तेमाल करती हैं।

Note: title matches को body matches से ऊपर rank करने के लिए weights इस्तेमाल करें।

उदाहरण: Text indexes

bash
test> db.articles.createIndex({ title: "text", body: "text" }, { weights: { title: 10, body: 1 }, name: "articles_text" })
articles_text
test> db.articles.find({ $text: { $search: "index performance" } }).limit(2)
[
  { _id: 7, title: 'Index performance tips', body: '...' },
  { _id: 3, title: 'Schema design', body: 'Good indexes improve performance ...' }
]

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. दो text indexes बनाना
  2. substring search की उम्मीद करना
  3. language settings ignore करना
चैप्टर सारांश
  • प्रति collection एक text index
  • कई fields allowed हैं
  • weights ranking adjust करते हैं
  • $text और $search इससे query करते हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.