」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > MongoDB 聚合管道

MongoDB 聚合管道

發佈於2024-08-07
瀏覽:221

MongoDB Aggregation Pipelines

Hi, aliens! I am Pavan. So in this repository, I will explain all the aggregation stages in depth with basic examples. I will also include links to resources for further learning.

So this repository contains JSON files for various MongoDB aggregation pipelines. These pipelines demonstrate how to use different aggregation stages and operations to process and analyze data.

Table of Contents

  • Introduction
  • CRUD Operations
  • Aggregation Stages
    • $match
    • $group
    • $project
    • $sort
    • $limit
    • $skip
    • $lookup
    • $unwind
    • $addFields
    • $replaceRoot
  • Aggregation Operations
    • $sum
    • $avg
    • $min
    • $max
    • $first
    • $last
  • Example Datasets
  • Resources for Further Learning

Introduction

Aggregation in MongoDB is a powerful way to process and analyze data stored in collections. It allows you to perform operations like filtering, grouping, sorting, and transforming data.

CRUD Operations

Create

db.orders.insertOne({
  "order_id": 26,
  "cust_id": 1006,
  "status": "A",
  "amount": 275,
  "items": ["apple", "banana"],
  "date": "2023-01-26"
});

Read

db.orders.find().pretty();

Update

db.orders.updateOne(
  { "order_id": 2 },
  {
    $set: { "status": "C", "amount": 500 },
    $currentDate: { "lastModified": true }
  }
);

Delete

db.orders.deleteOne({ "order_id": 1 });

Aggregation Stages

$match

Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

db.orders.aggregate([
  { $match: { "status": "A" } }
]);

$group

Groups input documents by the specified _id expression and for each distinct grouping, outputs a document. The _id field contains the unique group by value.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      totalSpent: { $sum: "$amount" }
    }
  }
]);

$project

Passes along the documents with the requested fields to the next stage in the pipeline.

db.orders.aggregate([
  { $project: { "order_id": 1, "items": 1, "_id": 0 } }
]);

$sort

Sorts all input documents and returns them to the pipeline in sorted order.

db.orders.aggregate([
  { $sort: { "amount": -1 } }
]);

$limit

Limits the number of documents passed to the next stage in the pipeline.

db.orders.aggregate([
  { $limit: 5 }
]);

$skip

Skips the first n documents and passes the remaining documents to the next stage in the pipeline.

db.orders.aggregate([
  { $skip: 5 }
]);

$lookup

Performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing.

db.orders.aggregate([
  {
    $lookup: {
      from: "orderDetails",
      localField: "order_id",
      foreignField: "order_id",
      as: "details"
    }
  }
]);

$unwind

Deconstructs an array field from the input documents to output a document for each element.

db.orders.aggregate([
  { $unwind: "$items" }
]);

$addFields

Adds new fields to documents.

db.orders.aggregate([
  { $addFields: { totalWithTax: { $multiply: ["$amount", 1.1] } } }
]);

$replaceRoot

Replaces the input document with the specified document.

db.orders.aggregate([
  { $replaceRoot: { newRoot: "$items" } }
]);

Aggregation Operations

$sum

Calculates and returns the sum of numeric values. $sum ignores non-numeric values.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      totalSpent: { $sum: "$amount" }
    }
  }
]);

$avg

Calculates and returns the average value of the numeric values.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      averageSpent: { $avg: "$amount" }
    }
  }
]);

$min

Returns the minimum value from the numeric values.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      minSpent: { $min: "$amount" }
    }
  }
]);

$max

Returns the maximum value from the numeric values.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      maxSpent: { $max: "$amount" }
    }
  }
]);

$first

Returns the first value from the documents for each group.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      firstOrder: { $first: "$amount" }
    }
  }
]);

$last

Returns the last value from the documents for each group.

db.orders.aggregate([
  {
    $group: {
      _id: "$cust_id",
      lastOrder: { $last: "$amount" }
    }
  }
]);

Example Datasets

Example documents used for performing CRUD and aggregation operations:

[
  { "order_id": 1, "cust_id": 1001, "status": "A", "amount": 250, "items": ["apple", "banana"], "date": "2023-01-01" },
  { "order_id": 2, "cust_id": 1002, "status": "B", "amount": 450, "items": ["orange", "grape"], "date": "2023-01-02" },
  { "order_id": 3, "cust_id": 1001, "status": "A", "amount": 300, "items": ["apple", "orange"], "date": "2023-01-03" },
  { "order_id": 4, "cust_id": 1003, "status": "A", "amount": 150, "items": ["banana", "grape"], "date": "2023-01-04" },
  { "order_id": 5, "cust_id": 1002, "status": "C", "amount": 500, "items": ["apple", "banana"], "date": "2023-01-05" },
  { "order_id": 6, "cust_id": 1004, "status": "A", "amount": 350, "items": ["orange", "banana"], "date": "2023-01-06" },
  { "order_id": 7, "cust_id": 1005, "status": "B", "amount": 200, "items": ["grape", "banana"], "date": "2023-01-07" },
  { "order_id": 8, "cust_id": 1003, "status": "A", "amount": 100, "items": ["apple", "orange"], "date": "2023-01-08" },
  { "order_id": 9, "cust_id": 1004, "status": "C", "amount": 400, "items": ["banana", "grape"], "date": "2023-01-09" },
  { "order_id": 10, "cust_id": 1001, "status": "A", "amount": 250, "items": ["apple", "grape"], "date": "2023-01-10" },
  { "order_id": 11, "cust_id": 1002, "status": "B", "amount": 350, "items": ["orange", "banana"], "date": "2023-01-11" },
  { "order_id": 12, "cust_id": 1003, "status": "A", "amount": 450, "items": ["apple", "orange"], "date": "2023-01-12" },
  { "order_id": 13, "cust_id": 1005, "status": "A", "amount": 150, "items": ["banana", "grape"], "date": "2023-01-13" },
  { "order_id": 14, "cust_id": 1004, "status": "C

", "amount": 500, "items": ["apple", "banana"], "date": "2023-01-14" },
  { "order_id": 15, "cust_id": 1002, "status": "A", "amount": 300, "items": ["orange", "grape"], "date": "2023-01-15" },
  { "order_id": 16, "cust_id": 1003, "status": "B", "amount": 200, "items": ["apple", "banana"], "date": "2023-01-16" },
  { "order_id": 17, "cust_id": 1001, "status": "A", "amount": 250, "items": ["orange", "grape"], "date": "2023-01-17" },
  { "order_id": 18, "cust_id": 1005, "status": "A", "amount": 350, "items": ["apple", "banana"], "date": "2023-01-18" },
  { "order_id": 19, "cust_id": 1004, "status": "C", "amount": 400, "items": ["orange", "grape"], "date": "2023-01-19" },
  { "order_id": 20, "cust_id": 1001, "status": "B", "amount": 150, "items": ["apple", "orange"], "date": "2023-01-20" },
  { "order_id": 21, "cust_id": 1002, "status": "A", "amount": 500, "items": ["banana", "grape"], "date": "2023-01-21" },
  { "order_id": 22, "cust_id": 1003, "status": "A", "amount": 450, "items": ["apple", "banana"], "date": "2023-01-22" },
  { "order_id": 23, "cust_id": 1004, "status": "B", "amount": 350, "items": ["orange", "banana"], "date": "2023-01-23" },
  { "order_id": 24, "cust_id": 1005, "status": "A", "amount": 200, "items": ["grape", "banana"], "date": "2023-01-24" },
  { "order_id": 25, "cust_id": 1001, "status": "A", "amount": 300, "items": ["apple", "orange"], "date": "2023-01-25" }
]

Resources for Further Learning

  • MongoDB Aggregation Documentation
  • MongoDB University Courses
  • MongoDB Aggregation Pipeline Builder

Feel free to clone this repository and experiment with the aggregation pipelines provided. If you have any questions or suggestions, please open an issue or submit a pull request.

$group

Groups orders by status and calculates the total amount and average amount for each status.

db.orders.aggregate([
  {
    $group: {
      _id: "$status",
      totalAmount: { $sum: "$amount" },
      averageAmount: { $avg: "$amount" }
    }
  }
]);

$project

Projects the order ID, customer ID, and a calculated field for the total amount with tax (assuming 10% tax).

db.orders.aggregate([
  {
    $project: {
      "order_id": 1,
      "cust_id": 1,
      "totalWithTax": { $multiply: ["$amount", 1.1] }
    }
  }
]);

$sort

Sorts orders first by status in ascending order and then by amount in descending order.

db.orders.aggregate([
  { $sort: { "status": 1, "amount": -1 } }
]);

$limit

Limits the result to the top 3 orders with the highest amount.

db.orders.aggregate([
  { $sort: { "amount": -1 } },
  { $limit: 3 }
]);

$skip

Skips the first 5 orders and returns the rest.

db.orders.aggregate([
  { $skip: 5 }
]);

$lookup

Joins the orders collection with an orderDetails collection to add order details.

db.orders.aggregate([
  {
    $lookup: {
      from: "orderDetails",
      localField: "order_id",
      foreignField: "order_id",
      as: "details"
    }
  }
]);

$unwind

Deconstructs the items array in each order to output a document for each item.

db.orders.aggregate([
  { $unwind: "$items" }
]);

$addFields

Adds a new field discountedAmount which is 90% of the original amount.

db.orders.aggregate([
  { $addFields: { discountedAmount: { $multiply: ["$amount", 0.9] } } }
]);

$replaceRoot

Replaces the root document with the items array.

db.orders.aggregate([
  { $replaceRoot: { newRoot: "$items" } }
]);

$sum

Calculates the total amount for all orders.

db.orders.aggregate([
  {
    $group: {
      _id: null,
      totalAmount: { $sum: "$amount" }
    }
  }
]);

$avg

Calculates the average amount spent per order.

db.orders.aggregate([
  {
    $group: {
      _id: null,
      averageAmount: { $avg: "$amount" }
    }
  }
]);

$min

Finds the minimum amount spent on an order.

db.orders.aggregate([
  {
    $group: {
      _id: null,
      minAmount: { $min: "$amount" }
    }
  }
]);

$max

Finds the maximum amount spent on an order.

db.orders.aggregate([
  {
    $group: {
      _id: null,
      maxAmount: { $max: "$amount" }
    }
  }
]);

$first

Gets the first order placed (by date).

db.orders.aggregate([
  { $sort: { "date": 1 } },
  {
    $group: {
      _id: null,
      firstOrder: { $first: "$$ROOT" }
    }
  }
]);

$last

Gets the last order placed (by date).

db.orders.aggregate([
  { $sort: { "date": -1 } },
  {
    $group: {
      _id: null,
      lastOrder: { $last: "$$ROOT" }
    }
  }
]);

So, we have covered basic CRUD operations, all major aggregation stages, and operations, and looked into resources for further learning.

版本聲明 本文轉載於:https://dev.to/bpk45_0670a02e0f3a6839b3a/mongodb-aggregation-pipelines-25mc?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • SOLID 原則使用一些有趣的類比與車輛範例
    SOLID 原則使用一些有趣的類比與車輛範例
    SOLID 是電腦程式設計中五個良好原則(規則)的縮寫。 SOLID 允許程式設計師編寫更易於理解和稍後更改的程式碼。 SOLID 通常與使用物件導向設計的系統一起使用。 讓我們使用車輛範例來解釋 SOLID 原理。想像一下,我們正在設計一個系統來管理不同類型的車輛,例如汽車和...
    程式設計 發佈於2024-11-06
  • 如何從另一個非同步函數中的非同步函數返回解析值?
    如何從另一個非同步函數中的非同步函數返回解析值?
    如何從非同步函數傳回一個值? 在提供的程式碼中,init()方法傳回一個Promise,但getPostById() 方法嘗試直接存取 Promise 傳回的值。為了解決這個問題,需要修改 init() 方法,使其在 Promise 解析後傳回 getPostById() 的值。 更新後的程式碼如下...
    程式設計 發佈於2024-11-06
  • 了解如何使用 React 建立多人國際象棋遊戲
    了解如何使用 React 建立多人國際象棋遊戲
    Hello and welcome! ?? Today I bring a tutorial to guide you through building a multiplayer chess game using SuperViz. Multiplayer games require real-t...
    程式設計 發佈於2024-11-06
  • 如何使用 JavaScript 正規表示式驗證 DD/MM/YYYY 格式的日期?
    如何使用 JavaScript 正規表示式驗證 DD/MM/YYYY 格式的日期?
    使用JavaScript 正規表示式驗證DD/MM/YYYY 格式的日期驗證日期是程式設計中的常見任務,並且能夠確保日期採用特定格式至關重要。在 JavaScript 中,正規表示式提供了執行此類驗證的強大工具。 考慮用於驗證YYYY-MM-DD 格式日期的正規表示式模式:/^\d{4}[\/\-]...
    程式設計 發佈於2024-11-06
  • JavaScript 中的節流與去抖:初學者指南
    JavaScript 中的節流與去抖:初學者指南
    使用 JavaScript 時,過多的事件觸發器可能會降低應用程式的速度。例如,使用者調整瀏覽器視窗大小或在搜尋列中輸入內容可能會導致事件在短時間內重複觸發,進而影響應用程式效能。 這就是節流和去抖可以發揮作用的地方。它們可以幫助您管理在處理過於頻繁觸發的事件時呼叫函數的頻率。 ...
    程式設計 發佈於2024-11-06
  • 在 Go 中匯入私人 Bitbucket 儲存庫時如何解決 403 Forbidden 錯誤?
    在 Go 中匯入私人 Bitbucket 儲存庫時如何解決 403 Forbidden 錯誤?
    Go 從私有Bitbucket 儲存庫匯入問題排查(403 禁止)使用go get 指令從Bitbucket.org 匯入私人儲存庫可能會遇到403 Forbidden 錯誤。若要解決此問題,請依照下列步驟操作:1.建立 SSH 連線:確保您已設定 SSH 金鑰並且能夠使用 SSH 連線至 Bitb...
    程式設計 發佈於2024-11-06
  • Singleton 和原型 Spring Bean 範圍:詳細探索
    Singleton 和原型 Spring Bean 範圍:詳細探索
    当我第一次开始使用 Spring 时,最让我感兴趣的概念之一是 bean 范围的想法。 Spring 提供了各种 bean 作用域,用于确定在 Spring 容器内创建的 bean 的生命周期。最常用的两个范围是 Singleton 和 Prototype。了解这些范围对于设计高效且有效的 Spri...
    程式設計 發佈於2024-11-06
  • 如何有效平滑雜訊資料曲線?
    如何有效平滑雜訊資料曲線?
    優化平滑雜訊曲線考慮近似的資料集:import numpy as np x = np.linspace(0, 2*np.pi, 100) y = np.sin(x) np.random.random(100) * 0.2這包括 20% 的變化。 UnivariateSpline 和移動平均線等方...
    程式設計 發佈於2024-11-06
  • 如何在 MySQL 中為有序序列值重新編號主索引?
    如何在 MySQL 中為有序序列值重新編號主索引?
    為有序序列值重新編號主索引如果您的MySQL 表的主索引(id) 以不一致的順序出現(例如,1、 31, 35, 100),您可能會想要將它們重新排列成連續的系列(1, 2, 3, 4)。 要實現此目的,您可以採用以下方法而不建立臨時表:SET @i = 0; UPDATE table_name S...
    程式設計 發佈於2024-11-06
  • 增強的物​​件文字
    增強的物​​件文字
    ES6引進了3種編寫物件字面量的方法 第一種方法: - ES6 Enhanced object literal syntax can take an external object like salary object and make it a property of the developer...
    程式設計 發佈於2024-11-06
  • 將 Tailwind 配置為設計系統
    將 Tailwind 配置為設計系統
    对于设计系统来说,一致性和理解性就是一切。一个好的设计系统通过实现它的代码的配置来确保实现的一致性。它需要是: 易于理解,无需放弃良好设计所需的细微差别; 可扩展和可维护,且不影响一致性。 使用我的 React 和 Tailwind 的默认堆栈,我将向您展示如何设置自己的版式、颜色和间距默认值,而不...
    程式設計 發佈於2024-11-06
  • 如何根據條件替換 Pandas DataFrame 列中的特定值?
    如何根據條件替換 Pandas DataFrame 列中的特定值?
    Pandas DataFrame:基於條件的目標值取代在Pandas中,通常需要根據某些條件修改DataFrame中的特定值。雖然常見的方法是使用 loc 來選擇行,但了解如何精確定位特定列進行值修改至關重要。 考慮以下 DataFrame,我們希望在其中替換“第一季”中的值超過 1990 且整數為...
    程式設計 發佈於2024-11-06
  • 如何修正 CentOS 7 上的 Yum Baseurl 問題
    如何修正 CentOS 7 上的 Yum Baseurl 問題
    _CentOS 7 Yum Error: Cannot Find a Valid Baseurl for Repo:base/7/x86_64_ 嘿夥計們, 遇到錯誤 can't find a valid baseurl for repo:base/7/x86_64 可能會非常令人沮喪,特別...
    程式設計 發佈於2024-11-06
  • 為什麼從模板化函數呼叫成員函數模板需要「template」關鍵字?
    為什麼從模板化函數呼叫成員函數模板需要「template」關鍵字?
    從範本函式呼叫範本類別的成員函式在提供的程式碼片段中,從另一個範本呼叫成員函式範本會導致編譯錯誤。具體來說,程式碼嘗試從 g() 內部呼叫 A::f()。但是,由於語法問題,此操作失敗。 要解決此問題,必須在成員函數呼叫之前明確指定模板關鍵字。這是因為根據C '03 標準14.2/4,當成員...
    程式設計 發佈於2024-11-06
  • 開發與雲端無關的應用程式
    開發與雲端無關的應用程式
    介紹 最近,我開始從事一個個人項目,我想建立一個與雲端無關的應用程式 - 即它可以部署在任何雲端提供者上,只需最少/無需更改程式碼。主要要求是將業務邏輯與雲端提供者特定邏輯分開。 在這篇文章中,我想分享所遵循的方法。 建立一個具有用於在雲端中執行操作的抽象方法的介面。 建立雲...
    程式設計 發佈於2024-11-06

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3