কম্পিউটার টিউটোরিয়াল

MongoDB-তে অ্যারে প্রোপার্টিতে মান আছে কিনা চেক করা ($in ব্যবহার করে)

MongoDB-এ কোনো অ্যারে ফিল্ডে নির্দিষ্ট মান আছে কিনা চেক করতে $in অপারেটর ব্যবহার করা হয়।

নমুনা ডেটা তৈরি করা

valueInArrayDemo কালেকশনে তিনটি ডকুমেন্ট ইনসার্ট করি যাতে UserMessage নামে একটি স্ট্রিং অ্যারে থাকে।

> db.valueInArrayDemo.insertOne({"UserName":"John","UserMessage":["Hi","Hello","Bye"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd684cf7924bb85b3f48959") }
> db.valueInArrayDemo.insertOne({"UserName":"Larry","UserMessage":["Thank You","Amazing","Nice"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd684d27924bb85b3f4895a") }
> db.valueInArrayDemo.insertOne({"UserName":"Carol","UserMessage":["Awesome","Bye","Cool"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5cd684d87924bb85b3f4895b") }

সকল ডকুমেন্ট দেখানো

> db.valueInArrayDemo.find().pretty();

আউটপুট:

{ "_id" : ObjectId("5cd684cf7924bb85b3f48959"), "UserName" : "John", "UserMessage" : [ "Hi", "Hello", "Bye" ] }
{ "_id" : ObjectId("5cd684d27924bb85b3f4895a"), "UserName" : "Larry", "UserMessage" : [ "Thank You", "Amazing", "Nice" ] }
{ "_id" : ObjectId("5cd684d87924bb85b3f4895b"), "UserName" : "Carol", "UserMessage" : [ "Awesome", "Bye", "Cool" ] }

$in দিয়ে নির্দিষ্ট মান খুঁজা

UserMessage অ্যারেতে "Bye" আছে এমন ডকুমেন্ট খুঁজতে:

> db.valueInArrayDemo.find({UserMessage:{$in:["Bye"]}});

আউটপুট:

{ "_id" : ObjectId("5cd684cf7924bb85b3f48959"), "UserName" : "John", "UserMessage" : [ "Hi", "Hello", "Bye" ] }
{ "_id" : ObjectId("5cd684d87924bb85b3f4895b"), "UserName" : "Carol", "UserMessage" : [ "Awesome", "Bye", "Cool" ] }

নোট: $in অ্যারেতে যেকোনো একটি মান মেললে ডকুমেন্ট রিটার্ন করে। একাধিক মান দিয়ে খুঁজতে {$in: ["Bye", "Hello"]} লিখুন।

  1. MongoDB কোয়ারী: আইডি যদি ডকুমেন্ট ফিল্ড অ্যারে মানের সমান হয় তবে বাদ দেওয়া

  2. MongoDB-এ অ্যারেতে ডুপ্লিকেট চেক করা

  3. MongoDB-এ অ্যারে কোয়েরি করে নির্দিষ্ট ভ্যালু ফেচ করা

  4. C#-এ অ্যারেতে ভ্যালু আছে কিনা চেক করা (Array.Exists)