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.
पिछले topic में आपने $or Operator के बारे में पढ़ा और समझ , जिसमे हम multiple conditions में से किसी एक true होने पर documents fetch करते थे , लेकिन ज्यादा OR conditions होने पर $or Operator ज्यादा manageable नहीं दिखता है।
जैसे आप 3 - 4 OR conditions कुछ इस तरह से apply करेंगे।
db.students.find({ $or : [ {"course" : "CS"}, {"course" : "IT"}, {"course" : "Civil"} ] });
same condition को आप $in Operator
से ज्यादा simple और easy में apply कर सकते हैं।
●●●
MongoDB में $in operator
का use उन documents को find करने के लिए किया जाता है field की value दिए गए किसी एक Array values से match करती है।
जैसे ऊपर दिए गए OR conditions को कुछ इस तरह से easily कर सकते हैं।
db.students.find({ "course" : {$in : ["CS", "IT", "Civil"]} });
$in Operator
की SQL equivalent query कुछ इस तरह होगी ।
SELECT * FROM students WHERE course In ("CS", "IT", "Civil");
Suppose करो आपके पास एक Collection है students
नाम का जिसका data format कुछ इस तरह से है -
{ "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"), "name" : "Jogn", "Course" : "CS", "course_year" : 2018, "language" : ["c", "java", "python", "php"], "other_details" : { "Father_name" : "Ram Lal", "phone_no" : 8895321456, "age" : 32, "gender" : "Male", "Address" : "AGra, UP", } }
अब हम वो सभी students find करेंगे , जो students CS या IT में है , जिसके लिए query कुछ इस तरह से होगी।
db.students.find({ "course" : {$in : ["CS", "IT" ]} });
आप Array field जैसे language
को भी भी $in
के साथ use कर सकते हैं।
नीचे दिए गए example में वो सभी documents select होंगे जो students , pass किये गए Array की किसी भी value से match करते होंगे।
db.students.find({ "language" : {$in : ["CS", "IT", "Civil"]} });
●●●
आप किसी Object value पर भी इसी तरह से $in operator को apply कर सकते हो।
जैसे other_details
एक object value को hold किये है , इसमें आगर हम 22 , 25 , 30 age के documents को कुछ इस तरह से find करेंगे।
db.students.find({ "other_details.age" : {$in : [22 , 25 , 30]} });
●●●
Agar एक students की specific list की age को update करना चाहते हैं तो आप $in operator का use कुछ इस तरह से करेंगे।
db.students.update( { "course": { $in : ["Civil", "CS"] } }, { $set: { "course_year" : 2023} }, { multi: true } )
इसकी SQL equivalent query कुछ इस तरह होगी ।
UPDATE students SET course_year=2023 WHERE course In ("Civil", "CS");
●●●
आप $in operator का use , delete operation में भी कर सकते हैं , Suppose आपको उन specific students को delete करना है , जिनका course Civil
या Mechanical
है।
तो delete operation कुछ इस तरह होगा।
db.students.remove({ "course" : { $in: ["Civil", "Mechanical"] } });
SQL equivalent query कुछ इस तरह होगी ।
DELETE FROM students WHERE course In ("Civil", "Mechanical");
●●●
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