嗨,外星人!我是帕万。因此,在这个存储库中,我将通过基本示例深入解释所有聚合阶段。我还将提供进一步学习资源的链接。
所以这个存储库包含各种 MongoDB 聚合管道的 JSON 文件。这些管道演示了如何使用不同的聚合阶段和操作来处理和分析数据。
MongoDB 中的聚合是处理和分析存储在集合中的数据的强大方法。它允许您执行过滤、分组、排序和转换数据等操作。
db.orders.insertOne({ "order_id": 26, "cust_id": 1006, "status": "A", "amount": 275, "items": ["apple", "banana"], "date": "2023-01-26" });
db.orders.find().pretty();
db.orders.updateOne( { "order_id": 2 }, { $set: { "status": "C", "amount": 500 }, $currentDate: { "lastModified": true } } );
db.orders.deleteOne({ "order_id": 1 });
过滤文档,仅将符合指定条件的文档传递到下一个管道阶段。
db.orders.aggregate([ { $match: { "status": "A" } } ]);
按指定的 _id 表达式对输入文档进行分组,并针对每个不同的分组输出一个文档。 _id 字段包含唯一的分组依据值。
db.orders.aggregate([ { $group: { _id: "$cust_id", totalSpent: { $sum: "$amount" } } } ]);
将带有请求字段的文档传递到管道的下一阶段。
db.orders.aggregate([ { $project: { "order_id": 1, "items": 1, "_id": 0 } } ]);
对所有输入文档进行排序,并按排序顺序将它们返回到管道。
db.orders.aggregate([ { $sort: { "amount": -1 } } ]);
限制传递到管道中下一阶段的文档数量。
db.orders.aggregate([ { $limit: 5 } ]);
跳过前 n 个文档,并将剩余文档传递到管道中的下一阶段。
db.orders.aggregate([ { $skip: 5 } ]);
对同一数据库中的另一个集合执行左外连接,以过滤“已连接”集合中的文档进行处理。
db.orders.aggregate([ { $lookup: { from: "orderDetails", localField: "order_id", foreignField: "order_id", as: "details" } } ]);
从输入文档中解构数组字段,以输出每个元素的文档。
db.orders.aggregate([ { $unwind: "$items" } ]);
向文档添加新字段。
db.orders.aggregate([ { $addFields: { totalWithTax: { $multiply: ["$amount", 1.1] } } } ]);
用指定文档替换输入文档。
db.orders.aggregate([ { $replaceRoot: { newRoot: "$items" } } ]);
计算并返回数值的总和。 $sum 忽略非数字值。
db.orders.aggregate([ { $group: { _id: "$cust_id", totalSpent: { $sum: "$amount" } } } ]);
计算并返回数值的平均值。
db.orders.aggregate([ { $group: { _id: "$cust_id", averageSpent: { $avg: "$amount" } } } ]);
返回数值中的最小值。
db.orders.aggregate([ { $group: { _id: "$cust_id", minSpent: { $min: "$amount" } } } ]);
返回数值中的最大值。
db.orders.aggregate([ { $group: { _id: "$cust_id", maxSpent: { $max: "$amount" } } } ]);
返回每个组文档中的第一个值。
db.orders.aggregate([ { $group: { _id: "$cust_id", firstOrder: { $first: "$amount" } } } ]);
返回每个组文档中的最后一个值。
db.orders.aggregate([ { $group: { _id: "$cust_id", lastOrder: { $last: "$amount" } } } ]);
用于执行 CRUD 和聚合操作的示例文档:
[ { "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" } ]
按状态对订单进行分组,并计算每个状态的总金额和平均金额。
db.orders.aggregate([ { $group: { _id: "$status", totalAmount: { $sum: "$amount" }, averageAmount: { $avg: "$amount" } } } ]);
投影订单 ID、客户 ID 和含税总额的计算字段(假设税费为 10%)。
db.orders.aggregate([ { $project: { "order_id": 1, "cust_id": 1, "totalWithTax": { $multiply: ["$amount", 1.1] } } } ]);
首先按状态升序对订单进行排序,然后按金额降序排序。
db.orders.aggregate([ { $sort: { "status": 1, "amount": -1 } } ]);
将结果限制为金额最高的前 3 个订单。
db.orders.aggregate([ { $sort: { "amount": -1 } }, { $limit: 3 } ]);
跳过前 5 个订单并返回其余订单。
db.orders.aggregate([ { $skip: 5 } ]);
将订单集合与 orderDetails 集合连接起来以添加订单详细信息。
db.orders.aggregate([ { $lookup: { from: "orderDetails", localField: "order_id", foreignField: "order_id", as: "details" } } ]);
Deconstructs the items array in each order to output a document for each item.
db.orders.aggregate([ { $unwind: "$items" } ]);
Adds a new field discountedAmount which is 90% of the original amount.
db.orders.aggregate([ { $addFields: { discountedAmount: { $multiply: ["$amount", 0.9] } } } ]);
Replaces the root document with the items array.
db.orders.aggregate([ { $replaceRoot: { newRoot: "$items" } } ]);
Calculates the total amount for all orders.
db.orders.aggregate([ { $group: { _id: null, totalAmount: { $sum: "$amount" } } } ]);
Calculates the average amount spent per order.
db.orders.aggregate([ { $group: { _id: null, averageAmount: { $avg: "$amount" } } } ]);
Finds the minimum amount spent on an order.
db.orders.aggregate([ { $group: { _id: null, minAmount: { $min: "$amount" } } } ]);
Finds the maximum amount spent on an order.
db.orders.aggregate([ { $group: { _id: null, maxAmount: { $max: "$amount" } } } ]);
Gets the first order placed (by date).
db.orders.aggregate([ { $sort: { "date": 1 } }, { $group: { _id: null, firstOrder: { $first: "$$ROOT" } } } ]);
Gets the last order placed (by date).
db.orders.aggregate([ { $sort: { "date": -1 } }, { $group: { _id: null, lastOrder: { $last: "$$ROOT" } } } ]);
以上是MongoDB 聚合管道的详细内容。更多信息请关注PHP中文网其他相关文章!