লজিক: NOT X AND NOT Y = NOT (X AND Y)
MongoDB-তে $nor বা $or এর সাথে $ne ব্যবহার করে NOT AND লজিক বাস্তবায়ন করা যায়।
সিনট্যাক্স (De Morgan's Law)
NOT (X AND Y) ≡ (NOT X) OR (NOT Y)
MongoDB: { $or: [ { X: { $ne: value1 } }, { Y: { $ne: value2 } } ] }
উদাহরণ: ডকুমেন্ট তৈরি
> db.NotAndDemo.insertOne({"StudentName":"John","StudentCountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c98746a330fd0aa0d2fe4a8") }
> db.NotAndDemo.insertOne({"StudentName":"John","StudentCountryName":"UK"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c987478330fd0aa0d2fe4a9") }
> db.NotAndDemo.insertOne({"StudentName":"David","StudentCountryName":"AUS"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c987487330fd0aa0d2fe4aa") }
> db.NotAndDemo.insertOne({"StudentName":"Chris","StudentCountryName":"UK"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9874ac330fd0aa0d2fe4ab") }
> db.NotAndDemo.insertOne({"StudentName":"Chris","StudentCountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9874b7330fd0aa0d2fe4ac") }
কুয়েরি: NOT (StudentName="Chris" AND StudentCountryName="US")
অর্থাৎ: Chris নামের US বাসিন্দা বাদে সবাই।
> db.NotAndDemo.find({
$or: [
{ "StudentName": { $ne: "Chris" } },
{ "StudentCountryName": { $ne: "US" } }
]
}).pretty();
আউটপুট: Chris-US ডকুমেন্ট বাদে বাকি ৪টি।
বিকল্প: MongoDB 3.6+ এ $nor বা $not ও ব্যবহার করা যায়, কিন্তু $or + $ne সব ভার্সনে কাজ করে।