$in অপারেটর ব্যবহার করে একাধিক ভ্যালু মিলানো
SQL-এ WHERE column IN (1, 2, 3) এর MongoDB সমতুল্য হলো { field: { $in: [value1, value2, ...] } }।
সিনট্যাক্স
db.yourCollectionName.find({
yourFieldName: { $in: [yourValue1, yourValue2, ...] }
}).pretty();
উদাহরণ
> db.whereInDemo.insertOne({"StudentName":"John","StudentMathScore":57});
> db.whereInDemo.insertOne({"StudentName":"Larry","StudentMathScore":89});
> db.whereInDemo.insertOne({"StudentName":"Chris","StudentMathScore":98});
> db.whereInDemo.insertOne({"StudentName":"Robert","StudentMathScore":99});
> db.whereInDemo.insertOne({"StudentName":"Bob","StudentMathScore":97});
StudentMathScore 97, 98, বা 99 হলে সিলেক্ট
> db.whereInDemo.find({ StudentMathScore: { $in: [97, 98, 99] } }).pretty();
{
"_id" : ObjectId("5ca281fd6304881c5ce84ba7"),
"StudentName" : "Chris", "StudentMathScore" : 98
}
{
"_id" : ObjectId("5ca2820a6304881c5ce84ba8"),
"StudentName" : "Robert", "StudentMathScore" : 99
}
{
"_id" : ObjectId("5ca282206304881c5ce84ba9"),
"StudentName" : "Bob", "StudentMathScore" : 97
}
বিপরীত ($nin): লিস্টে নাই এমনগুলো পেতে $nin (not in) ব্যবহার করুন।
টিপস: $in ইনডেক্স ব্যবহার করতে পারে। অ্যারে ফিল্ডে থাকলে অ্যারেতে যেকোনো এলিমেন্ট ম্যাচ করলে ডকুমেন্ট রিটার্ন হয়।