MongoDB $in Operator In HIndi | $in Operator In MongoDB In Hindi

📔 : MongoDB 🔗

पिछले 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

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");

MongoDB $in Example

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"]}
});

Use $in to find in Object value

आप किसी 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]}
});

Update operation using $in operator

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");

Delete operation using $in operator

आप $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");

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers