”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > MongoDB 聚合管道

MongoDB 聚合管道

发布于2024-08-07
浏览:898

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]删除
最新教程 更多>
  • 如何从另一个异步函数中的异步函数返回解析值?
    如何从另一个异步函数中的异步函数返回解析值?
    如何从异步函数返回一个值?在提供的代码中,init()方法返回一个Promise,但是getPostById() 方法尝试直接访问 Promise 返回的值。为了解决这个问题,需要修改 init() 方法,使其在 Promise 解析后返回 getPostById() 的值。更新后的代码如下:cla...
    编程 发布于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 连接到 B...
    编程 发布于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 ...
    编程 发布于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 在保存 CSV 时添加索引列?
    如何防止 Pandas 在保存 CSV 时添加索引列?
    避免使用 Pandas 保存的 CSV 中的索引列使用 Pandas 进行修改后保存 csv 文件时,默认行为是包含索引列。为了避免这种情况,可以在使用 to_csv() 方法时将索引参数设置为 False。为了详细说明,请考虑以下命令序列:pd.read_csv('C:/Path/to/file....
    编程 发布于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