MongoDB aggregate() In Hindi | What is aggregate() In MongoDB In Hindi

📔 : MongoDB 🔗

Normally , database operation perform करने के लिए तो already MongoDB में कई सारे useful methods और operators तो हैं। लेकिन इन सबके अलावा MongoDB में एक aggregate() Method भी है जिसका use करके आप हर तरह कि query का use करके records find कर सकते हैं।

और इस topic में हम aggregate() Method के बारे में ही बात करेंगे।

What is aggregate() In MongoDB

aggregate() method MongoDB में एक powerful method / tool है जो data को manipulate और transform करने के लिए use किया जा सकता है। इसका use aggregation framework के अंदर किया जाता है।

Aggregation framework एक pipeline के रूप में काम करता है , जहां पर आप एक stages की series को define करते हैं और हर stage एक specific data transformation को represent करती है।

MongoDB aggregate() Syntax

db.collection_name.aggregate(pipeline)

यहां collection_name वो collection है जिस पर आप aggregation apply करना चाहते हैं और pipeline एक array है जिसमे आप aggregation के अलग - अलग stages को define करते हैं।

MongoDB Pipeline Concept

aggregate() method एक pipeline को follow करता है , ये pipeline stages की series को define करता है। जिसमे हर stage एक specific operation perform करती है।

pipeline एक तरह से कई stages का array है जिसमे हम need के according query operators का use करते हैं।

Pipeline Stages In MongoDB 

Pipeline में हर stage एक specific task execute करता है , कुछ common और important stages नीचे दिए गए है।

  1. $match : इस stage में आप documents को specific criteria के bases पर filter करते हैं , जैसे कि आप documents को date range या age के according filter कर सकते हैं। इसे एक तरह से conditions Apply करने के लिए use किया जाता है , जिसमे $or , $and , $gt , $lt या बाकि logical query operators का use कर सकते हैं।

  2. $group : इस stage में documents को एक group में organize किया जाता है , यह SQL के GROUP BY Statement की तरह ही है। आप data को group करने के लिए किसी एक या एक से अधिक field का use कर सकते हैं।

  3. $project : इस stage में आप documents के fields को choose करते हैं जो documents से fetch करने हैं , आप इनका format या manipulation भी कर सकते हैं।

  4. $sort : इस stage से documents को किसी field के basis पर sort किया जाता है।

  5. $unwind : यह एक array field को अलग - अलग documents में convert करता है , इसका use mainly तब होता है जब आप एक document के अंदर किसी array field को query करते हैं और आप चाहते हैं कि हर array element एक अलग document के रूप में आये।

MongoDB aggregate() Use Cases

aggregate() method के कुछ use cases नीचे दिए गए हैं।

  1. Data Aggregation : आप data को aggregate कर सकते हैं जैसे कि sales data को month-wise total calculate करना।

  2. Data Transformation : Data को एक format से दूसरे format में convert करने में use किया जा सकता है , जैसे कि date format change करना।

  3. Data Filtering : Documents को specific criteria के basis पर filter करने के लिए use किया जा सकता है।

  4. Data Grouping : Documents को एक field के basis पर group करके उस group के लिए calculations कर सकते हैं।

MongoDB aggregate() Example

नीचे एक basic example है जिसमे sales data को month-wise total calculate किया गया है।

db.sales.aggregate([
  {
    $match: {
      date: { $gte: ISODate("2023-01-01"), $lte: ISODate("2023-12-31") }
    }
  },
  {
    $group: {
      _id: { $month: "$date" },
      totalSales: { $sum: "$amount" }
    }
  },
  {
    $sort: { _id: 1 }
  }
]);

इस example में , $match stage से documents को date range के basis पर filter किया गया , फिर $group stage से months के हिसाब से total sales calculate किया गया और उसके बाद $sort stage से results को sort किया गया।

हो सकता है अभी आपको aggregate() method के बारे में बहुत clear नहीं हुआ हो , लेकिन आगे जब आप बाकी stages operators को deeply पढ़ेंगे तो समझ आ जायगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers