← Back to MongoDB Course | Chapter 6: Query Operators | Lesson 7 of 7

Text Search के Basics

एक text index $text queries enable करता है जो stemming और scoring के साथ string fields में words search करती हैं।

In this page:

  1. Text Search Basics
Syntax
javascript
db.collection_name.createIndex({ field: "text" })
db.collection_name.find({ $text: { $search: "words" } })

Text Search Basics

एक या ज़्यादा string fields पर प्रति collection एक text index बनाएँ, फिर { $text: { $search: "words" } } से query करें। Results textScore meta projection से relevance से sort हो सकते हैं।

Text search word stemming और stop words handle करता है लेकिन partial words नहीं; Atlas Search richer options offer करता है।

Note: एक collection का सिर्फ एक text index हो सकता है।

उदाहरण: Text search basics

bash
test> db.articles.createIndex({ title: "text", body: "text" })
title_text_body_text
test> db.articles.insertMany([
...   { _id: 1, title: "Learning MongoDB", body: "Documents and collections" },
...   { _id: 2, title: "Cooking pasta", body: "Boil water" }
... ])
test> db.articles.find({ $text: { $search: "mongodb documents" } }, { title: 1, score: { $meta: "textScore" } })
[ { _id: 1, title: 'Learning MongoDB', score: 1.3333333333333333 } ]

⚠️ 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 index के $text query करना
  2. partial word matches की उम्मीद करना
  3. एक collection पर कई text indexes बनाना
चैप्टर सारांश
  • पहले एक text index बनाएँ
  • $text और $search से query करें
  • Relevance के लिए textScore से sort करें
  • प्रति collection सिर्फ एक text index
🔒

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.