Element Operators ($exists $type) की जानकारी
Element operators test करते हैं कि एक field exist करती है या नहीं या इसका BSON type क्या है।
In this page:
Syntax
{ field: { $exists: true } }
{ field: { $type: "string" } }
Element Operators ($exists $type)
{ field: { $exists: true } } उन documents को match करता है जिनमें field है, भले ही इसकी value null हो। { field: { $type: "string" } } BSON type name या number से match करता है। ये flexible collections में dirty data spot करने के लिए handy हैं।
Note:
$exists: true फिर भी null set की गई fields match करता है।
उदाहरण: Element operators ($exists $type)
test> db.things.insertMany([{ _id: 1, v: "text" }, { _id: 2, v: 42 }, { _id: 3, v: null }, { _id: 4 }])
{
acknowledged: true,
insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4 }
}
test> db.things.find({ v: { $exists: true } }, { _id: 1 })
[ { _id: 1 }, { _id: 2 }, { _id: 3 } ]
test> db.things.find({ v: { $exists: false } }, { _id: 1 })
[ { _id: 4 } ]
test> db.things.find({ v: { $type: "string" } }, { _id: 1 })
[ { _id: 1 } ]
test> db.things.find({ v: null }, { _id: 1 })
[ { _id: 3 }, { _id: 4 } ]
⚠️ 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. #}
आम गलतियां
- $exists false को null समझना
- BSON types की बजाय JavaScript typeof names इस्तेमाल करना
- यह भूल जाना कि arrays element types पर match करते हैं
चैप्टर सारांश
- $exists presence test करता है
- null values फिर भी exist करती हैं
- $type BSON types test करता है
- Data quality checks के लिए great
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: