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

Element Operators ($exists $type) की जानकारी

Element operators test करते हैं कि एक field exist करती है या नहीं या इसका BSON type क्या है।
Syntax
javascript
{ 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)

bash
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. #}
आम गलतियां
  1. $exists false को null समझना
  2. BSON types की बजाय JavaScript typeof names इस्तेमाल करना
  3. यह भूल जाना कि 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:

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.