Header Ads

Header ADS

MongoDB part-3(Aggregation)

 

MySQL এর aggregate function এবং MongoDB এর aggregation প্রক্রিয়া একই নয়, তবে তারা কিছুটা সমতুল্য। আসুন তাদের মধ্যে কিছু মৌলিক পার্থক্য দেখি:

MySQL এর Aggregate Function

MySQL এ aggregate function হলো এমন কিছু ফাংশন যা এক বা একাধিক রেকর্ডের উপর কাজ করে এবং একটি একক মান প্রদান করে। উদাহরণস্বরূপ:

  • COUNT(): নির্দিষ্ট কলামের মধ্যে মোট রেকর্ড গননা করে।
  • SUM(): সংখ্যার উপর ভিত্তি করে মোট যোগফল দেয়।
  • AVG(): গড় মান নির্ধারণ করে।
  • MAX() এবং MIN(): সর্বোচ্চ এবং সর্বনিম্ন মান নির্ধারণ করে।

এই ফাংশনগুলো সাধারণত GROUP BY ক্লজের সঙ্গে ব্যবহৃত হয়।

MongoDB এর Aggregation

MongoDB তে aggregation প্রক্রিয়া বেশ জটিল এবং নমনীয়। এখানে বিভিন্ন ধরণের pipeline stages ব্যবহার করে ডেটা প্রসেসিং করা হয়। কিছু উদাহরণ:

  • $match: নির্দিষ্ট শর্ত পূরণকারী ডেটা নির্বাচন করে।
  • $group: ডেটাকে গ্রুপ করে এবং aggregate operation প্রয়োগ করে (যেমন COUNT, SUM, AVG)।
  • $project: প্রয়োজনীয় ক্ষেত্রগুলো নির্বাচন এবং নতুন ক্ষেত্র তৈরি করে।

তুলনা

  • ডেটার প্রক্রিয়াকরণ: MySQL এ aggregate function সাধারণত সরল এবং ডেটাবেসের সাপেক্ষে কাজ করে, যেখানে MongoDB এর aggregation pipeline আরও জটিল এবং নিয়মিত প্রসেসিংয়ের সুযোগ দেয়।
  • ফাংশনালিটি: MongoDB এর aggregation ফিচার আরো বেশি ফ্লেক্সিবল, যেমন একাধিক স্টেজ ব্যবহার করে জটিল ডেটা প্রসেসিং করা সম্ভব।

সংক্ষেপে, উভয় সিস্টেমেই aggregate function এর মাধ্যমে ডেটা প্রক্রিয়াকরণের সুযোগ রয়েছে, তবে তাদের কার্যকরী পদ্ধতি এবং ব্যবহারে কিছু মৌলিক পার্থক্য রয়েছে।


*********************************************************************************

MongoDB aggregation is a powerful framework used for processing and transforming data within collections. It allows you to perform complex data manipulations and analytics by combining multiple operations into a pipeline.

Key Concepts

  1. Pipeline: An aggregation pipeline is a sequence of stages that process documents. Each stage performs an operation on the input documents and passes the results to the next stage.

  2. Aggregation Stages

    1. $match: Filters documents based on a specified condition.
    2. $group: Groups documents by a specified identifier and performs aggregations.
    3. $sort: Sorts documents in ascending or descending order.
    4. $project: Reshapes documents (selects or excludes fields).
    5. $limit: Limits the number of documents passed to the next stage.
    6. $skip: Skips a specified number of documents.
    7. $unwind: Deconstructs an array field to output a document for each element.
    8. $lookup: Performs a left outer join with another collection.
    9. $out: Writes the results of the aggregation pipeline to a specified collection.
    10. $count: Counts the number of documents that pass through the pipeline.

    3. Aggregation Operators

    1. $avg: Calculates the average value of a numeric field.
    2. $sum: Calculates the total sum of a numeric field.
    3. $max: Returns the maximum value of a field.
    4. $min: Returns the minimum value of a field.
    5. $push: Adds a value to an array in the output document.
    6. $addToSet: Adds a value to an array only if it doesn't already exist.
    7. $first: Returns the first value of a field in the group.
    8. $last: Returns the last value of a field in the group.
    9. $stdDevPop: Calculates the population standard deviation.
    10. $stdDevSamp: Calculates the sample standard deviation.

    Summary

    • Stages are used to define the steps in the aggregation pipeline that process the documents.
    • Operators are used within stages (like $group) to perform specific calculations or transformations on the data.


# Basic Syntax :

db.collectionName.aggregate([

        { $stage1: { /* stage1 options */ } },

        { $stage2: { /* stage2 options */ } },

])


# Example : 

Assume we have a collection named students with the following documents:

{ "_id": 1, "name": "Alice", "age": 20, "score": 85 } { "_id": 2, "name": "Bob", "age": 21, "score": 90 } { "_id": 3, "name": "Charlie", "age": 22, "score": 75 } { "_id": 4, "name": "David", "age": 20, "score": 95 }

Aggregation Example

Goal: Calculate the average score of students grouped by age.

db.students.aggregate([ { $group: { _id: "$age", averageScore: { $avg: "$score" } } }, { $sort: { _id: 1 } } // Sort by age ])

Explanation of the Example

  1. $group: Groups the documents by the age field (_id: "$age"), calculating the average score for each age group.

    • averageScore: { $avg: "$score" }: Computes the average score.
  2. $sort: Sorts the output documents by age in ascending order.

# Output : 

{ "_id": 20, "averageScore": 90 }
{ "_id": 21, "averageScore": 90 }
{ "_id": 22, "averageScore": 75 }






Created By _____  🅰🅻🅸  🅷🅰🅸🅳🅰🆁







Powered by Blogger.