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

Comparison Operators की जानकारी

Comparison operators values को numbers, strings, या dates के against test करते हैं।

In this page:

  1. Comparison Operators
Syntax
javascript
{ field: { $eq: value } }
{ field: { $ne: value } }
{ field: { $gte: value, $lt: value } }
{ field: { $in: [value1, value2] } }

Comparison Operators

$eq, $ne, $gt, $gte, $lt, $lte, $in, और $nin ज़्यादातर comparisons cover करते हैं। Comparisons BSON type ordering follow करते हैं, इसलिए numbers numbers जैसी दिखने वाली strings से match नहीं करते। Dates chronologically compare होती हैं।

Note: एक string "30" number 30 से match नहीं करती।

उदाहरण: Comparison operators

bash
test> db.scores.insertMany([{ _id: 1, s: 50 }, { _id: 2, s: "50" }, { _id: 3, s: 75 }, { _id: 4 }])
{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4 }
}
test> db.scores.find({ s: 50 })
[ { _id: 1, s: 50 } ]
test> db.scores.find({ s: { $gt: 60 } })
[ { _id: 3, s: 75 } ]
test> db.scores.find({ s: { $ne: 50 } })
[ { _id: 2, s: '50' }, { _id: 3, s: 75 }, { _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. strings की तरह stored numbers compare करना
  2. $ne इस्तेमाल करना और यह भूल जाना कि यह missing fields भी match करता है
  3. dates को strings की तरह compare करना
चैप्टर सारांश
  • $eq $ne $gt $gte $lt $lte जैसे operators मिलते हैं
  • Lists के लिए $in और $nin
  • Types match होने चाहिए
  • $ne missing fields भी match करता है
🔒

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.