If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
MongoDB कई logical query operators provide करता है , उन्ही में से एक है $and
operator . $and operator का use logical AND
operations perform करने के लिए किया जाता है।
$and
operator MongoDB में एक query operator है जो multiple conditions को combine करने में use होता है। इसका primary use case एक query में दो या दो से ज्यादा conditions define करना है।
और MongoDB सिर्फ वो ही documents return करेगा जो इन सभी conditions को satisfy करते हैं।
●●●
{ $and: [ { condition1 }, { condition2 }, { condition3 }... ] }
हर एक condition एक JSON
object होता है , जिसमे आप field के साथ एक comparison operator और value specify करते हैं , हालाँकि need के according direct value देने की जगह पर बाकी operators जैसे $gt
, $gte
, $lt
, $lte
etc . का use भी किया जाता है।
Suppose आपके database में एक students
नाम का collection है , जिसमे age , name , sex और class fields हैं।
अब हम वो students select करेंगे , जिनका sex : Male
और class : 8
है , जिसके लिए query कुछ इस तरह से होगी।
db.studenta.find({$and: [{"sex" : "Male"}, {"class" : "8"}]})
ऊपर दी गयी MongoDB query का SQL equivalent इस तरह होगा।
SELECT * FROM students WHERE sex = "Male" AND class = "8";
shell में find() method by default documents को non-structured format में show करता है इसलिए formatted way में documents को show करने के लिए आप pretty() method का use कर सकते हैं।
db.studenta.find({$and: [{"sex" : "Male"}, {"class" : "8"}]}).pretty()
●●●
जैसा कि आपने अभी ऊपर पढ़ा कि , need के according direct value देने की जगह पर बाकी operators जैसे $gt , $gte, $lt , $lte etc . का use भी किया जाता है।
नीचे दिए गए example में उन सभी Male students को fetch किया गया है जिनकी age 12 या उससे ज्यादा है ।
db.students.find({$and: [{"sex" : "Male"}, {"age" : { $gte: 12 }}]})
दी गयी MongoDB query का SQL equivalent इस तरह होगा।
SELECT * FROM students WHERE sex = "Male" AND age >= 12;
ठीक इसी तरह से आप बाकी operators का use भी $and operator के साथ कर सकते है।
$and operator का use आप documents को find, update और delete करने के लिए कर सकते हैं।
●●●
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