MongoDB $or Operator In HIndi | MongoDB $or Example

📔 : MongoDB 🔗

MongoDB कई logical query operators provide करता है , उन्ही में से एक है $or operator . $and operator का use logical OR operations perform करने के लिए किया जाता है।

MongoDB $or

$or operator MongoDB में एक query operator है , जो multiple conditions को combine करने में use होता है। इसका primary use case एक query में दो या दो से ज्यादा conditions define करना है।

$or operator logical "OR operations" को performs करता है। यह operator वही documents को retrieve करता है जो दिए गए expressions में से किसी एक condition से match करें।

MongoDB $or Syntax

db.collection.find({
   $or: [
      { condition1 },
      { condition2 },
      // Aur aap jitni chahein conditions add kar sakte hain.
   ]
})

हर एक condition एक JSON object होता है , जिसमे आप field के साथ एक comparison operator और value specify करते हैं , हालाँकि need के according direct value देने की जगह पर बाकी operators जैसे $gt , $gte, $lt , $lte etc . का use भी किया जाता है।

MongoDB $or Example

Suppose आपके database में एक students नाम का collection है , जिसमे age , name , sex और class , course fields हैं।

अब हम वो students select करेंगे , जो students CS या IT में है , जिसके लिए query कुछ इस तरह से होगी।

db.students.find({
  $or : [
    {"course" : "CS"}, 
    {"course" : "IT"}
  ]
});

ऊपर दी गयी MongoDB query का SQL equivalent इस तरह होगा।

SELECT *
FROM students
WHERE course = "CS" OR course = "IT";

Finding values in an array using $or operator

आप चाहें तो value में array भी provide कर सकते हैं , दी गयी किसी एक array values से match करने पर वो document मिल जायगा। हालाँकि इसके लिए आपको $in operator का use करना पड़ेगा।

db.students.find({
  $or : [
    {"course" : ["CS", "IT", "Civil"]}
  ]
});

इसका SQL equivalent कुछ इस तरह होगा।

SELECT *
FROM students
WHERE course In ("CS", "IT", "Civil");

Use other operator with $or in MongoDB

जैसा कि आपने अभी ऊपर पढ़ा कि , need के according direct value देने की जगह पर बाकी operators जैसे $gt , $gte, $lt , $lte etc . का use भी किया जाता है।

नीचे दिए गए example में उन सभी Male students को fetch किया गया है जिनकी age 12 course IT है। उससे ज्यादा है ।

नीचे दिए गए example में उन सभी students को fetch किया गया है जिनकी age 12 या sex Male है ।

db.students.find({
  $or : [
    {"sex" : "Male"}, 
    {
      "age" : {$eq : 12}
    }
  ]
});

SQL equivalent

SELECT *
FROM students
WHERE sex = "Male" OR age = 12;

$or operator का use आप documents को find, update और delete करने के लिए कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers