কম্পিউটার

সমষ্টিগত ফলাফল পান এবং বিভিন্ন MongoDB ডকুমেন্টে পুনরাবৃত্তি মানের গণনা খুঁজুন


বিভিন্ন নথিতে বারবার মান গণনা পেতে, aggregate() ব্যবহার করুন। আসুন নথি-

সহ একটি সংগ্রহ তৈরি করি
> db.demo452.insertOne({"StudentName":"John","StudentAge":21});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e7b7e3371f552a0ebb0a6f3")
}
> db.demo452.insertOne({"StudentName":"John","StudentAge":22});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e7b7e3671f552a0ebb0a6f4")
}
> db.demo452.insertOne({"StudentName":"John","StudentAge":23});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e7b7e3971f552a0ebb0a6f5")
}
> db.demo452.insertOne({"StudentName":"David","StudentAge":24});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e7b7e4371f552a0ebb0a6f6")
}
> db.demo452.insertOne({"StudentName":"David","StudentAge":25});{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e7b7e4571f552a0ebb0a6f7")
}

Find() পদ্ধতি -

এর সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করুন
> db.demo452.find();

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
{ "_id" : ObjectId("5e7b7e3371f552a0ebb0a6f3"), "StudentName" : "John", "StudentAge" : 21 }
{ "_id" : ObjectId("5e7b7e3671f552a0ebb0a6f4"), "StudentName" : "John", "StudentAge" : 22 }
{ "_id" : ObjectId("5e7b7e3971f552a0ebb0a6f5"), "StudentName" : "John", "StudentAge" : 23 }
{ "_id" : ObjectId("5e7b7e4371f552a0ebb0a6f6"), "StudentName" : "David", "StudentAge" : 24}
{ "_id" : ObjectId("5e7b7e4571f552a0ebb0a6f7"), "StudentName" : "David", "StudentAge" : 25}

বিভিন্ন MongoDB নথিতে পুনরাবৃত্তি করা মানের গণনা খুঁজে বের করার জন্য নিচের প্রশ্নটি রয়েছে -

> db.demo452.aggregate([
...    {$group: {_id:"$StudentName", count:{$sum:1}}},
...    {$sort: {count:-1}},
...
...    {$group: {_id:1, StudentName:{$push:{StudentName:"$_id", count:"$count"}}}},
...    {$project: {
...       first : {$arrayElemAt: ["$StudentName", 0]},
...       second: {$arrayElemAt: ["$StudentName", 1]},
...       others: {$slice:["$StudentName", 2, {$size: "$StudentName"}]}
...    }
... },
...
... {$project: {
...    status: [
...       "$first",
...       "$second",
...       {
...          StudentName: "New Student Name",
...          count: {$sum: "$others.count"}
...       }
...    ]
... }
... },
...
... {$unwind: "$status"},
... {$project: { _id:0, StudentName: "$status.StudentName", count: "$status.count" }}
... ])

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
{ "StudentName" : "John", "count" : 3 }
{ "StudentName" : "David", "count" : 2 }
{ "StudentName" : "New Student Name", "count" : 0 }

  1. স্বতন্ত্র মানের ঘটনা গণনা করতে এবং একটি নতুন কলামে ফলাফল প্রদর্শন করতে MySQL ক্যোয়ারী?

  2. শর্তের উপর ভিত্তি করে মান গণনা করুন এবং MySQL এর সাথে বিভিন্ন কলামে ফলাফল প্রদর্শন করবেন?

  3. একাধিক সারি গণনা করুন এবং MySQL সহ বিভিন্ন কলামে (এবং একটি একক সারি) ফলাফল প্রদর্শন করুন

  4. C++ এ 1, 3 এবং 4 এর যোগফল হিসাবে N প্রকাশ করার বিভিন্ন উপায়ে গণনা