$match का उपयोग
$match find जैसी same query syntax इस्तेमाल करते हुए एक pipeline में documents filter करता है।
In this page:
Syntax
{ $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
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. #}
आम गलतियां
- देर से $match इस्तेमाल करना
- बिना $expr के aggregation expressions इस्तेमाल करना
- यह भूल जाना कि 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: