নথিতে সাব-অ্যারে থেকে সর্বোচ্চ মান খুঁজে পেতে, আপনি একটি সমষ্টিগত কাঠামো ব্যবহার করতে পারেন। আসুন প্রথমে ডকুমেন্ট সহ একটি সংগ্রহ তৈরি করি
> db.findHighestValueDemo.insertOne(
... {
... _id: 10001,
... "StudentDetails": [
... { "StudentName": "Chris", "StudentMathScore": 56},
... { "StudentName": "Robert", "StudentMathScore":47 },
... { "StudentName": "John", "StudentMathScore": 98 }]
... }
... );
{ "acknowledged" : true, "insertedId" : 10001 }
> db.findHighestValueDemo.insertOne(
... {
... _id: 10002,
... "StudentDetails": [
... { "StudentName": "Ramit", "StudentMathScore": 89},
... { "StudentName": "David", "StudentMathScore":76 },
... { "StudentName": "Bob", "StudentMathScore": 97 }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 10002 } Find() পদ্ধতি
এর সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করার জন্য নিম্নলিখিত প্রশ্ন রয়েছে> db.findHighestValueDemo.find().pretty();
এটি নিম্নলিখিত আউটপুট তৈরি করবে
{
"_id" : 10001,
"StudentDetails" : [
{
"StudentName" : "Chris",
"StudentMathScore" : 56
},
{
"StudentName" : "Robert",
"StudentMathScore" : 47
},
{
"StudentName" : "John",
"StudentMathScore" : 98
}
]
}
{
"_id" : 10002,
"StudentDetails" : [
{
"StudentName" : "Ramit",
"StudentMathScore" : 89
},
{
"StudentName" : "David",
"StudentMathScore" : 76
},
{
"StudentName" : "Bob",
"StudentMathScore" : 97
}
]
} নথিতে সাব-অ্যারেগুলি থেকে সর্বোচ্চ মান খুঁজে পাওয়ার জন্য নিম্নোক্ত ক্যোয়ারী
> db.findHighestValueDemo.aggregate([
... {$project:{"StudentDetails.StudentName":1, "StudentDetails.StudentMathScore":1}},
... {$unwind:"$StudentDetails"},
... {$sort:{"StudentDetails.StudentMathScore":-1}},
... {$limit:1}
... ]).pretty(); এটি নিম্নলিখিত আউটপুট তৈরি করবে
{
"_id" : 10001,
"StudentDetails" : {
"StudentName" : "John",
"StudentMathScore" : 98
}
}