← Back to MongoDB Course | Chapter 7: Aggregation | Lesson 2 of 7

$match का उपयोग

$match find जैसी same query syntax इस्तेमाल करते हुए एक pipeline में documents filter करता है।

In this page:

  1. $match
Syntax
javascript
{ $match: { field: value } }

$match

$match सिर्फ वे documents रखता है जो इसकी query satisfy करते हैं और इन्हें आगे pass करता है। यह regular query operators इस्तेमाल करता है।

एक pipeline की शुरुआत में यह indexes इस्तेमाल कर सकता है, इसलिए जब possible हो इसे पहले रखें।

$expr में wrap न होने तक यह aggregation expressions इस्तेमाल नहीं कर सकता।

Note: एक pipeline में एक early $match सबसे सस्ता performance win है।

उदाहरण: $match

bash
test> db.orders.insertMany([
...   { _id: 1, status: "paid", total: 50 }, { _id: 2, status: "open", total: 20 },
...   { _id: 3, status: "paid", total: 80 }, { _id: 4, status: "cancelled", total: 15 }
... ])
{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4 }
}
test> db.orders.aggregate([{ $match: { status: "paid", total: { $gt: 60 } } }])
[ { _id: 3, status: 'paid', total: 80 } ]
test> db.orders.aggregate([{ $match: { status: { $in: ["open", "cancelled"] } } }, { $count: "n" }])
[ { n: 2 } ]

⚠️ 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. देर से $match इस्तेमाल करना
  2. बिना $expr के aggregation expressions इस्तेमाल करना
  3. यह भूल जाना कि fields case sensitive हैं
चैप्टर सारांश
  • $match query syntax इस्तेमाल करता है
  • पहले रखना best है
  • शुरुआत में indexes इस्तेमाल कर सकता है
  • Expressions के लिए $expr इस्तेमाल करें
🔒

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.