MongoDB $ne Operator In Hindi | $ne operator in MongoDB In Hindi

📔 : MongoDB 🔗

पिछले topic में आपने में $eq Operator के बारे में पढ़ा, इस topic में हम $ne Operator के बारे में समझेंगे।

What is $ne operator in MongoDB ?

$ne का मतलब है not equals to (!=) , और MongoDB में $ne operator एक query operator है जो एक field की value को एक specific दी गयी user value के साथ compare करने के लिए use होता है। यह check करता है कि field की value दी गयी value के बराबर नहीं है।

$ne operator का use documents को filter करने के लिए किया जाता है जिससे हम documents को find , update या delete कर सकते हैं।

MongoDB $ne Syntax

{ field : { $ne: value } }

यहां , field वो field / key है जिसकी value compare करनी है , और value वो value है जिससे आप field / key की value के साथ compare करना चाहते हैं , यह check करेगा कि field की value दी की value के बराबर नहीं है।

MongoDB $ne Example

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

हम वो सभी students find करेंगे जिनका course CS नहीं है।

db.students.find({
"course" : {$ne : "CS"}
})

इस MongoDB query का SQL equivalent query कुछ इस तरह से होगी।

SELECT * FROM students WHERE course != "CS";

// or

SELECT * FROM students WHERE course IS NOT "CS";

MongoDB update documents with $ne Operator

ऊपर दिया गया example तो documents को find करने का था , हालाँकि same condition को update Query के साथ भी use कर सकते हैं।

db.students.updateOne(
{"course" : {$ne : "CS"} } , 
{"$set" : {"grade" : "A+"} }
)

SQL equivalent query

UPDATE students set grade='A+' WHERE course != "CS" 

MongoDB delete documents with $ne Operator

और इसी तरह से documents को delete भी कर सकते हैं।

db.students.deleteMany({"course" : {$ne : "CS"}})

SQL equivalent query

DELETE FROM students WHERE course != "CS" 

Use other operator with $nq in MongoDB

$eq operator को आप Logical operators जैसे $and , $or या $in के साथ multi conditions के रूप में भी use कर सकते हैं।

1. नीचे दी गयी query में वो सभी documents retrieve किये गए हैं जहाँ 'status' "active" नहीं है और 'category' "electronics" नहीं है।

db.students.find({
  $and: [
    { status: { $ne : "active" } },
    { category: { $ne : "electronics" } }
  ]
})

2. $eq operator example with $or operator

db.students.find({
  $or: [
    { age: { $ne : 25 } },
    { age: { $ne : 30 } }
  ]
})

तो कुछ इस तरह से MongoDB में $ne operator का use किया जाता है , I Hope आपको समझ आया होगा ।

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