শর্তসাপেক্ষ আপসার্ট বা আপডেটের জন্য, আপনি $max অপারেটর ব্যবহার করতে পারেন। আসুন প্রথমে ডকুমেন্ট সহ একটি সংগ্রহ তৈরি করি
>db.conditionalUpdatesDemo.insertOne({"_id":100,"StudentFirstScore":89,"StudentSecondScore":78,"BiggestScore":89});
{ "acknowledged" : true, "insertedId" : 100 }
>db.conditionalUpdatesDemo.insertOne({"_id":101,"StudentFirstScore":305,"StudentSecondScore":560,"BiggestScore":1050});
{ "acknowledged" : true, "insertedId" : 101 }
>db.conditionalUpdatesDemo.insertOne({"_id":103,"StudentFirstScore":560,"StudentSecondScore":789,"BiggestScore":880});
{ "acknowledged" : true, "insertedId" : 103 }
Following is the query to display all documents from a collection with the help of find() method:
> db.conditionalUpdatesDemo.find().pretty(); এটি নিম্নলিখিত আউটপুট তৈরি করবে
{
"_id" : 100,
"StudentFirstScore" : 89,
"StudentSecondScore" : 78,
"BiggestScore" : 89
}
{
"_id" : 101,
"StudentFirstScore" : 305,
"StudentSecondScore" : 560,
"BiggestScore" : 1050
}
{
"_id" : 103,
"StudentFirstScore" : 560,
"StudentSecondScore" : 789,
"BiggestScore" : 880
} MongoDB
-এ শর্তসাপেক্ষ আপসার্ট বা আপডেটের জন্য নিম্নলিখিত ক্যোয়ারী দেওয়া হল> db.conditionalUpdatesDemo.update( { _id: 100 }, { $max: { "BiggestScore": 150 } } );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) আসুন আমরা ক্ষেত্রটি পরীক্ষা করি "বিগস্টস্কোর" 150 মান দিয়ে আপডেট করা হয়েছে নাকি _id 100 এর জন্য নয়
> db.conditionalUpdatesDemo.find().pretty();
এটি নিম্নলিখিত আউটপুট তৈরি করবে
{
"_id" : 100,
"StudentFirstScore" : 89,
"StudentSecondScore" : 78,
"BiggestScore" : 150
}
{
"_id" : 101,
"StudentFirstScore" : 305,
"StudentSecondScore" : 560,
"BiggestScore" : 1050
}
{
"_id" : 103,
"StudentFirstScore" : 560,
"StudentSecondScore" : 789,
"BiggestScore" : 880
}