This tutorial illustrates how to construct an aggregation pipeline, perform the aggregation on a collection, and display the results using the language of your choice.本教程说明了如何构建聚合管道,对集合执行聚合,并使用您选择的语言显示结果。
About This Task关于此任务
This tutorial demonstrates how to create insights from customer order data. The results show the list of products ordered that cost more than $15. Each document contains the number of units sold and the total sale value for each product.本教程演示了如何从客户订单数据中创建见解。结果显示,订购的产品价格超过15美元。每份文件都包含每种产品的销售量和总销售价值。
The aggregation pipeline performs the following operations:聚合管道执行以下操作:
Unwinds an array field into separate documents将数组字段拆分为单独的文档Matches a subset of documents by a field value根据字段值匹配文档子集Groups documents by common field values按公共字段值对文档进行分组Adds computed fields to each result document将计算字段添加到每个结果文档中
Before You Begin开始之前
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Shell.使用右上角的“选择语言”下拉菜单设置以下示例的语言,或选择MongoDB Shell。
MongoDB Shell
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection, use the insertMany() method:orders集合,请使用insertMany()方法:
db.orders.insertMany( [
{
order_id: 6363763262239,
products: [
{
prod_id: "abc12345",
name: "Asus Laptop",
price: 431
},
{
prod_id: "def45678",
name: "Karcher Hose Set",
price: 22
}
]
},
{
order_id: 1197372932325,
products: [
{
prod_id: "abc12345",
name: "Asus Laptop",
price: 429
}
]
},
{
order_id: 9812343774839,
products: [
{
prod_id: "pqr88223",
name: "Morphy Richards Food Mixer",
price: 431
},
{
prod_id: "def45678",
name: "Karcher Hose Set",
price: 21
}
]
},
{
order_id: 4433997244387,
products: [
{
prod_id: "def45678",
name: "Karcher Hose Set",
price: 23
},
{
prod_id: "jkl77336",
name: "Picky Pencil Sharpener",
price: 1
},
{
prod_id: "xyz11228",
name: "Russell Hobbs Chrome Kettle",
price: 16
}
]
}
] )C
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new C app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习此聚合教程之前,您必须设置一个新的C应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Get Started with the C Driver guide.要了解如何安装驱动程序并连接到MongoDB,请参阅C驱动程序入门指南。
To learn more about performing aggregations in the C Driver, see the Aggregation guide.要了解有关在C驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为agg-tutorial.c. Paste the following code in this file to create an app template for the aggregation tutorials.agg-tutorial.c的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
int main(void)
{
mongoc_init();
// Replace the placeholder with your connection string.
char *uri = "<connection string>";
mongoc_client_t* client = mongoc_client_new(uri);
// Get a reference to relevant collections.
// ... mongoc_collection_t *some_coll = mongoc_client_get_collection(client, "agg_tutorials_db", "some_coll");
// ... mongoc_collection_t *another_coll = mongoc_client_get_collection(client, "agg_tutorials_db", "another_coll");
// Delete any existing documents in collections if needed.
// ... {
// ... bson_t *filter = bson_new();
// ... bson_error_t error;
// ... if (!mongoc_collection_delete_many(some_coll, filter, NULL, NULL, &error))
// ... {
// ... fprintf(stderr, "Delete error: %s\n", error.message);
// ... }
// ... bson_destroy(filter);
// ... }
// Insert sample data into the collection or collections.
// ... {
// ... size_t num_docs = ...;
// ... bson_t *docs[num_docs];
// ...
// ... docs[0] = ...;
// ...
// ... bson_error_t error;
// ... if (!mongoc_collection_insert_many(some_coll, (const bson_t **)docs, num_docs, NULL, NULL, &error))
// ... {
// ... fprintf(stderr, "Insert error: %s\n", error.message);
// ... }
// ...
// ... for (int i = 0; i < num_docs; i++)
// ... {
// ... bson_destroy(docs[i]);
// ... }
// ... }
{
const bson_t *doc;
// Add code to create pipeline stages.
bson_t *pipeline = BCON_NEW("pipeline", "[",
// ... Add pipeline stages here.
"]");
// Run the aggregation.
// ... mongoc_cursor_t *results = mongoc_collection_aggregate(some_coll, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
bson_destroy(pipeline);
// Print the aggregation results.
while (mongoc_cursor_next(results, &doc))
{
char *str = bson_as_canonical_extended_json(doc, NULL);
printf("%s\n", str);
bson_free(str);
}
bson_error_t error;
if (mongoc_cursor_error(results, &error))
{
fprintf(stderr, "Aggregation error: %s\n", error.message);
}
mongoc_cursor_destroy(results);
}
// Clean up resources.
// ... mongoc_collection_destroy(some_coll);
mongoc_client_destroy(client);
mongoc_cleanup();
return EXIT_SUCCESS;
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the C Get Started guide.要了解如何定位部署的连接字符串,请参阅《C入门》指南的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
char *uri = "mongodb+srv://mongodb-example:27017";Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
mongoc_collection_t *orders = mongoc_client_get_collection(client, "agg_tutorials_db", "orders");
{
bson_t *filter = bson_new();
bson_error_t error;
if (!mongoc_collection_delete_many(orders, filter, NULL, NULL, &error))
{
fprintf(stderr, "Delete error: %s\n", error.message);
}
bson_destroy(filter);
}
{
size_t num_docs = 4;
bson_t *docs[num_docs];
docs[0] = BCON_NEW(
"order_id", BCON_INT64(6363763262239),
"products", "[",
"{",
"prod_id", BCON_UTF8("abc12345"),
"name", BCON_UTF8("Asus Laptop"),
"price", BCON_INT32(431),
"}",
"{",
"prod_id", BCON_UTF8("def45678"),
"name", BCON_UTF8("Karcher Hose Set"),
"price", BCON_INT32(22),
"}",
"]");
docs[1] = BCON_NEW(
"order_id", BCON_INT64(1197372932325),
"products", "[",
"{",
"prod_id", BCON_UTF8("abc12345"),
"name", BCON_UTF8("Asus Laptop"),
"price", BCON_INT32(429),
"}",
"]");
docs[2] = BCON_NEW(
"order_id", BCON_INT64(9812343774839),
"products", "[",
"{",
"prod_id", BCON_UTF8("pqr88223"),
"name", BCON_UTF8("Morphy Richards Food Mixer"),
"price", BCON_INT32(431),
"}",
"{",
"prod_id", BCON_UTF8("def45678"),
"name", BCON_UTF8("Karcher Hose Set"),
"price", BCON_INT32(21),
"}",
"]");
docs[3] = BCON_NEW(
"order_id", BCON_INT64(4433997244387),
"products", "[",
"{",
"prod_id", BCON_UTF8("def45678"),
"name", BCON_UTF8("Karcher Hose Set"),
"price", BCON_INT32(23),
"}",
"{",
"prod_id", BCON_UTF8("jkl77336"),
"name", BCON_UTF8("Picky Pencil Sharpener"),
"price", BCON_INT32(1),
"}",
"{",
"prod_id", BCON_UTF8("xyz11228"),
"name", BCON_UTF8("Russell Hobbs Chrome Kettle"),
"price", BCON_INT32(16),
"}",
"]");
bson_error_t error;
if (!mongoc_collection_insert_many(orders, (const bson_t **)docs, num_docs, NULL, NULL, &error))
{
fprintf(stderr, "Insert error: %s\n", error.message);
}
for (int i = 0; i < num_docs; i++)
{
bson_destroy(docs[i]);
}
}C++11
Create the Template App创建模板应用程序
Before you begin following an aggregation tutorial, you must set up a new C++ app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习聚合教程之前,您必须设置一个新的C++应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Get Started with C++ tutorial.要了解如何安装驱动程序并连接到MongoDB,请参阅C++入门教程。
To learn more about using the C++ driver, see the API documentation.要了解有关使用C++驱动程序的更多信息,请参阅API文档。
To learn more about performing aggregations in the C++ Driver, see the Aggregation guide.要了解有关在C++驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为agg-tutorial.cpp. Paste the following code in this file to create an app template for the aggregation tutorials.agg-tutorial.cpp的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
using bsoncxx::builder::basic::make_array;
int main() {
mongocxx::instance instance;
// Replace the placeholder with your connection string.
mongocxx::uri uri("<connection string>");
mongocxx::client client(uri);
auto db = client["agg_tutorials_db"];
// Delete existing data in the database, if necessary.
db.drop();
// Get a reference to relevant collections.
// ... auto some_coll = db["..."];
// ... auto another_coll = db["..."];
// Insert sample data into the collection or collections.
// ... some_coll.insert_many(docs);
// Create an empty pipelne.
mongocxx::pipeline pipeline;
// Add code to create pipeline stages.
// pipeline.match(make_document(...));
// Run the aggregation and print the results.
auto cursor = orders.aggregate(pipeline);
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed) << std::endl;
}
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the C++ Get Started tutorial.要了解如何定位部署的连接字符串,请参阅C++入门教程的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
mongocxx::uri uri{"mongodb+srv://mongodb-example:27017"};Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
auto orders = db["orders"];
std::vector<bsoncxx::document::value> order_docs = {
bsoncxx::from_json(R"({
"order_id": 6363763262239,
"products": [
{
"prod_id": "abc12345",
"name": "Asus Laptop",
"price": 431
},
{
"prod_id": "def45678",
"name": "Karcher Hose Set",
"price": 22
}
]
})"),
bsoncxx::from_json(R"({
"order_id": 1197372932325,
"products": [
{
"prod_id": "abc12345",
"name": "Asus Laptop",
"price": 429
}
]
})"),
bsoncxx::from_json(R"({
"order_id": 9812343774839,
"products": [
{
"prod_id": "pqr88223",
"name": "Morphy Richards Food Mixer",
"price": 431
},
{
"prod_id": "def45678",
"name": "Karcher Hose Set",
"price": 21
}
]
})"),
bsoncxx::from_json(R"({
"order_id": 4433997244387,
"products": [
{
"prod_id": "def45678",
"name": "Karcher Hose Set",
"price": 23
},
{
"prod_id": "jkl77336",
"name": "Picky Pencil Sharpener",
"price": 1
},
{
"prod_id": "xyz11228",
"name": "Russell Hobbs Chrome Kettle",
"price": 16
}
]
})")
};
orders.insert_many(order_docs); // Might throw an exception可能会抛出异常C#
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new C#/.NET app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习此聚合教程之前,您必须设置一个新的C#/.NET应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the C#/.NET Driver Quick Start guide.要了解如何安装驱动程序并连接到MongoDB,请参阅C#/.NET驱动程序快速入门指南。
To learn more about performing aggregations in the C#/.NET Driver, see the Aggregation guide.要了解有关在C#/.NET驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, paste the following code into your 安装驱动程序后,将以下代码粘贴到Program.cs file to create an app template for the aggregation tutorials.Program.cs文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
// Define data model classes.
// ... public class MyClass { ... }
// Replace the placeholder with your connection string.
var uri = "<connection string>";
var client = new MongoClient(uri);
var aggDB = client.GetDatabase("agg_tutorials_db");
// Get a reference to relevant collections.
// ... var someColl = aggDB.GetCollection<MyClass>("someColl");
// ... var anotherColl = aggDB.GetCollection<MyClass>("anotherColl");
// Delete any existing documents in collections if needed.
// ... someColl.DeleteMany(Builders<MyClass>.Filter.Empty);
// Insert sample data into the collection or collections.
// ... someColl.InsertMany(new List<MyClass> { ... });
// Add code to chain pipeline stages to the Aggregate() method.
// ... var results = someColl.Aggregate().Match(...);
// Print the aggregation results.
foreach (var result in results.ToList())
{
Console.WriteLine(result);
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Set Up a Free Tier Cluster in Atlas step of the C# Quick Start guide.要了解如何定位部署的连接字符串,请参阅《C#快速入门》指南的在Atlas中设置免费层集群步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
var uri = "mongodb+srv://mongodb-example:27017";Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
First, create C# classes to model the data in the 首先,创建C#类来对orders collection:orders集合中的数据进行建模:
public class Order
{
[]
public ObjectId Id { get; set; }
public long OrderId { get; set; }
[]
public required List<Product> Products { get; set; }
}
public class OrderUnwound
{
public long OrderId { get; set; }
[]
public required Product Products { get; set; }
}
public class Product
{
[]
public required string ProductId { get; set; }
public string Name { get; set; } = "";
public int Price { get; set; }
}
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
var orders = aggDB.GetCollection<Order>("orders");
orders.InsertMany(new List<Order>
{
new Order
{
OrderId = 6363763262239L,
Products = new List<Product>
{
new Product
{
ProductId = "abc12345",
Name = "Asus Laptop",
Price = 431
},
new Product
{
ProductId = "def45678",
Name = "Karcher Hose Set",
Price = 22
}
}
},
new Order
{
OrderId = 1197372932325L,
Products = new List<Product>
{
new Product
{
ProductId = "abc12345",
Name = "Asus Laptop",
Price = 429
}
}
},
new Order
{
OrderId = 9812343774839L,
Products = new List<Product>
{
new Product
{
ProductId = "pqr88223",
Name = "Morphy Richards Food Mixer",
Price = 431
},
new Product
{
ProductId = "def45678",
Name = "Karcher Hose Set",
Price = 21
}
}
},
new Order
{
OrderId = 4433997244387L,
Products = new List<Product>
{
new Product
{
ProductId = "def45678",
Name = "Karcher Hose Set",
Price = 23
},
new Product
{
ProductId = "jkl77336",
Name = "Picky Pencil Sharpener",
Price = 1
},
new Product
{
ProductId = "xyz11228",
Name = "Russell Hobbs Chrome Kettle",
Price = 16
}
}
}
});Go
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new Go app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习此聚合教程之前,您必须设置一个新的Go应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Go Driver Quick Start guide.要了解如何安装驱动程序并连接到MongoDB,请参阅Go驱动程序快速入门指南。
To learn more about performing aggregations in the Go Driver, see the Aggregation guide.要了解有关在Go驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为agg_tutorial.go. Paste the following code in this file to create an app template for the aggregation tutorials.agg_tutorial.go的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// Define structs.
// type MyStruct struct { ... }
func main() {
ctx := context.Background()
// Replace the placeholder with your connection string.
const uri = "<connection string>"
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
log.Fatal(err)
}
defer func() {
if err = client.Disconnect(ctx); err != nil {
log.Fatal(err)
}
}()
aggDB := client.Database("agg_tutorials_db")
// Get a reference to relevant collections.
// ... someColl := aggDB.Collection("...")
// ... anotherColl := aggDB.Collection("...")
// Delete any existing documents in collections if needed.
// ... someColl.DeleteMany(cxt, bson.D{})
// Insert sample data into the collection or collections.
// ... _, err = someColl.InsertMany(...)
// Add code to create pipeline stages.
// ... myStage := bson.D{{...}}
// Create a pipeline that includes the stages.
// ... pipeline := mongo.Pipeline{...}
// Run the aggregation.
// ... cursor, err := someColl.Aggregate(ctx, pipeline)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := cursor.Close(ctx); err != nil {
log.Fatalf("failed to close cursor: %v", err)
}
}()
// Decode the aggregation results.
var results []bson.D
if err = cursor.All(ctx, &results); err != nil {
log.Fatalf("failed to decode results: %v", err)
}
// Print the aggregation results.
for _, result := range results {
res, _ := bson.MarshalExtJSON(result, false, false)
fmt.Println(string(res))
}
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a MongoDB Cluster step of the Go Quick Start guide.要了解如何定位部署的连接字符串,请参阅Go快速入门指南的创建MongoDB集群步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
const uri = "mongodb+srv://mongodb-example:27017";Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
First, create Go structs to model the data in the 首先,创建Go结构体来对orders collection:orders集合中的数据进行建模:
type Order struct {
OrderID int `bson:"order_id"`
Products []Product `bson:"products"`
}
type Product struct {
ProductID string `bson:"prod_id"`
Name string `bson:"name"`
Price int `bson:"price"`
}
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
orders := aggDB.Collection("orders")
_, err = orders.InsertMany(ctx, []interface{}{
Order{
OrderID: 6363763262239,
Products: []Product{
{ProductID: "abc12345", Name: "Asus Laptop", Price: 431},
{ProductID: "def45678", Name: "Karcher Hose Set", Price: 22},
},
},
Order{
OrderID: 1197372932325,
Products: []Product{
{ProductID: "abc12345", Name: "Asus Laptop", Price: 429},
},
},
Order{
OrderID: 9812343774839,
Products: []Product{
{ProductID: "pqr88223", Name: "Morphy Richards Food Mixer", Price: 431},
{ProductID: "def45678", Name: "Karcher Hose Set", Price: 21},
},
},
Order{
OrderID: 4433997244387,
Products: []Product{
{ProductID: "def45678", Name: "Karcher Hose Set", Price: 23},
{ProductID: "jkl77336", Name: "Picky Pencil Sharpene", Price: 1},
{ProductID: "xyz11228", Name: "Russell Hobbs Chrome Kettle", Price: 16},
},
},
})
if err != nil {
log.Fatal(err)
}Java(Sync)
Create the Template App创建模板应用程序
Before you begin following an aggregation tutorial, you must set up a new Java app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习聚合教程之前,您必须设置一个新的Java应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Get Started with the Java Driver guide.要了解如何安装驱动程序并连接到MongoDB,请参阅Java驱动程序入门指南。
To learn more about performing aggregations in the Java Sync Driver, see the Aggregation guide.要了解有关在Java同步驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为AggTutorial.java. Paste the following code in this file to create an app template for the aggregation tutorials.AggTutorial.java的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
package org.example;
// Modify imports for each tutorial as needed.根据需要修改每个教程的导入。
import com.mongodb.client.*;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Field;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.Variable;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class AggTutorial {
public static void main(String[] args) {
// Replace the placeholder with your connection string.
String uri = "<connection string>";
try (MongoClient mongoClient = MongoClients.create(uri)) {
MongoDatabase aggDB = mongoClient.getDatabase("agg_tutorials_db");
// Get a reference to relevant collections.
// ... MongoCollection<Document> someColl = ...
// ... MongoCollection<Document> anotherColl = ...
// Insert sample data into the collection or collections.
// ... someColl.insertMany(...);
// Create an empty pipeline array.
List<Bson> pipeline = new ArrayList<>();
// Add code to create pipeline stages.
// ... pipeline.add(...);
// Run the aggregation.
// ... AggregateIterable<Document> aggregationResult =
// someColl.aggregate(pipeline);
// Print the aggregation results.
for (Document document : aggregationResult) {
System.out.println(document.toJson());
}
}
}
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Java Sync Quick Start guide.要了解如何定位部署的连接字符串,请参阅《Java Sync快速入门》指南的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
String uri = "mongodb+srv://mongodb-example:27017";Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
MongoDatabase aggDB = mongoClient.getDatabase("agg_tutorials_db");
MongoCollection<Document> orders = aggDB.getCollection("orders");
orders.insertMany(
Arrays.asList(
new Document("order_id", 6363763262239f)
.append("products", Arrays.asList(
new Document("prod_id", "abc12345")
.append("name", "Asus Laptop")
.append("price", 431),
new Document("prod_id", "def45678")
.append("name", "Karcher Hose Set")
.append("price", 22)
)),
new Document("order_id", 1197372932325f)
.append("products", Collections.singletonList(
new Document("prod_id", "abc12345")
.append("name", "Asus Laptop")
.append("price", 429)
)),
new Document("order_id", 9812343774839f)
.append("products", Arrays.asList(
new Document("prod_id", "pqr88223")
.append("name", "Morphy Richards Food Mixer")
.append("price", 431),
new Document("prod_id", "def45678")
.append("name", "Karcher Hose Set")
.append("price", 21)
)),
new Document("order_id", 4433997244387f)
.append("products", Arrays.asList(
new Document("prod_id", "def45678")
.append("name", "Karcher Hose Set")
.append("price", 23),
new Document("prod_id", "jkl77336")
.append("name", "Picky Pencil Sharpener")
.append("price", 1),
new Document("prod_id", "xyz11228")
.append("name", "Russell Hobbs Chrome Kettle")
.append("price", 16)
))
)
);Kotlin(Coroutine)
Create the Template App创建模板应用程序
Before you begin following an aggregation tutorial, you must set up a new Kotlin app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习聚合教程之前,您必须设置一个新的Kotlin应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Kotlin Driver Quick Start guide.要了解如何安装驱动程序并连接到MongoDB,请参阅Kotlin驱动程序快速入门指南。
To learn more about performing aggregations in the Kotlin Driver, see the Aggregation guide.要了解有关在Kotlin驱动程序中执行聚合的更多信息,请参阅聚合指南。
In addition to the driver, you must also add the following dependencies to your 除了驱动程序,您还必须将以下依赖项添加到build.gradle.kts file and reload your project:build.gradle.kts文件中并重新加载项目:
dependencies {
// Implements Kotlin serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
// Implements Kotlin date and time handling
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
}
After you install the driver, create a file called 安装驱动程序后,创建一个名为AggTutorial.kt. Paste the following code in this file to create an app template for the aggregation tutorials.AggTutorial.kt的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
package org.example
// Modify imports for each tutorial as needed.
import com.mongodb.client.model.*
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDateTime
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import org.bson.Document
import org.bson.conversions.Bson
// Define data classes.
data class MyClass(
...
)
suspend fun main() {
// Replace the placeholder with your connection string.
val uri = "<connection string>"
MongoClient.create(uri).use { mongoClient ->
val aggDB = mongoClient.getDatabase("agg_tutorials_db")
// Get a reference to relevant collections.
// ... val someColl = ...
// Delete any existing documents in collections if needed.
// ... someColl.deleteMany(empty())
// Insert sample data into the collection or collections.
// ... someColl.insertMany( ... )
// Create an empty pipeline.
val pipeline = mutableListOf<Bson>()
// Add code to create pipeline stages.
// ... pipeline.add(...)
// Run the aggregation.
// ... val aggregationResult = someColl.aggregate<Document>(pipeline)
// Print the aggregation results.
aggregationResult.collect { println(it) }
}
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Connect to your Cluster step of the Kotlin Driver Quick Start guide.要了解如何定位部署的连接字符串,请参阅Kotlin驱动程序快速入门指南的连接到集群步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
val uri = "mongodb+srv://mongodb-example:27017"Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
First, create Kotlin data classes to model the data in the orders collection:
data class Order(
val orderID: Float,
val products: List<Product>
)
data class Product(
val prodID: String,
val name: String,
val price: Int
)
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
val orders = aggDB.getCollection<Order>("orders")
orders.deleteMany(Filters.empty())
orders.insertMany(
listOf(
Order(
6363763262239f, listOf(
Product("abc12345", "Asus Laptop", 431),
Product("def45678", "Karcher Hose Set", 22)
)
),
Order(
1197372932325f, listOf(
Product("abc12345", "Asus Laptop", 429)
)
),
Order(
9812343774839f, listOf(
Product("pqr88223", "Morphy Richards Food Mixer", 431),
Product("def45678", "Karcher Hose Set", 21)
)
),
Order(
4433997244387f, listOf(
Product("def45678", "Karcher Hose Set", 23),
Product("jkl77336", "Picky Pencil Sharpener", 1),
Product("xyz11228", "Russell Hobbs Chrome Kettle", 16)
)
)
)
)Node.js
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new Node.js app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习本聚合教程之前,您必须设置一个新的Node.js应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Node.js Driver Quick Start guide.要了解如何安装驱动程序并连接到MongoDB,请参阅Node.js驱动程序快速入门指南。
To learn more about performing aggregations in the Node.js Driver, see the Aggregation guide.要了解有关在Node.js驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file to run the tutorial template. Paste the following code in this file to create an app template for the aggregation tutorials.安装驱动程序后,创建一个文件来运行教程模板。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
const { MongoClient } = require('mongodb');
// Replace the placeholder with your connection string.
const uri = '<connection-string>';
const client = new MongoClient(uri);
export async function run() {
try {
const aggDB = client.db('agg_tutorials_db');
// Get a reference to relevant collections.
// ... const someColl =
// ... const anotherColl =
// Delete any existing documents in collections.
// ... await someColl.deleteMany({});
// Insert sample data into the collection or collections.
// ... const someData = [ ... ];
// ... await someColl.insertMany(someData);
// Create an empty pipeline array.
const pipeline = [];
// Add code to create pipeline stages.
// ... pipeline.push({ ... })
// Run the aggregation.
// ... const aggregationResult = ...
// Print the aggregation results.
for await (const document of aggregationResult) {
console.log(document);
}
} finally {
await client.close();
}
}
run().catch(console.dir);
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Node.js Quick Start guide.要了解如何定位部署的连接字符串,请参阅Node.js快速入门指南的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
const uri = "mongodb+srv://mongodb-example:27017";Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
const orders = aggDB.collection('orders');
await orders.insertMany([
{
order_id: 6363763262239,
products: [
{
prod_id: 'abc12345',
name: 'Asus Laptop',
price: 431,
},
{
prod_id: 'def45678',
name: 'Karcher Hose Set',
price: 22,
},
],
},
{
order_id: 1197372932325,
products: [
{
prod_id: 'abc12345',
name: 'Asus Laptop',
price: 429,
},
],
},
{
order_id: 9812343774839,
products: [
{
prod_id: 'pqr88223',
name: 'Morphy Richards Food Mixer',
price: 431,
},
{
prod_id: 'def45678',
name: 'Karcher Hose Set',
price: 21,
},
],
},
{
order_id: 4433997244387,
products: [
{
prod_id: 'def45678',
name: 'Karcher Hose Set',
price: 23,
},
{
prod_id: 'jkl77336',
name: 'Picky Pencil Sharpener',
price: 1,
},
{
prod_id: 'xyz11228',
name: 'Russell Hobbs Chrome Kettle',
price: 16,
},
],
},
]);PHP
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new PHP app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习本聚合教程之前,您必须设置一个新的PHP应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the PHP library and connect to MongoDB, see the Get Started with the PHP Library tutorial.要了解如何安装PHP库并连接到MongoDB,请参阅PHP库入门教程。
To learn more about performing aggregations in the PHP library, see the Aggregation guide.要了解有关在PHP库中执行聚合的更多信息,请参阅聚合指南。
After you install the library, create a file called 安装库后,创建一个名为agg_tutorial.php. Paste the following code in this file to create an app template for the aggregation tutorials.agg_tutorial.php的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
require 'vendor/autoload.php';
// Modify imports for each tutorial as needed.
use MongoDB\Client;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\Sort;
use MongoDB\Builder\Query;
use MongoDB\Builder\Expression;
use MongoDB\Builder\Accumulator;
use function MongoDB\object;
// Replace the placeholder with your connection string.
$uri = '<connection string>';
$client = new Client($uri);
// Get a reference to relevant collections.
// ... $someColl = $client->agg_tutorials_db->someColl;
// ... $anotherColl = $client->agg_tutorials_db->anotherColl;
// Delete any existing documents in collections if needed.
// ... $someColl->deleteMany([]);
// Insert sample data into the collection or collections.
// ... $someColl->insertMany(...);
// Add code to create pipeline stages within the Pipeline instance.
// ... $pipeline = new Pipeline(...);
// Run the aggregation.
// ... $cursor = $someColl->aggregate($pipeline);
// Print the aggregation results.
foreach ($cursor as $doc) {
echo json_encode($doc, JSON_PRETTY_PRINT), PHP_EOL;
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Get Started with the PHP Library tutorial.要了解如何定位部署的连接字符串,请参阅PHP库入门教程的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
$uri = 'mongodb+srv://mongodb-example:27017';Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
$orders = $client->agg_tutorials_db->orders;
$orders->deleteMany([]);
$orders->insertMany(
[
[
'order_id' => 6363763262239,
'products' => [
[
'prod_id' => 'abc12345',
'name' => 'Asus Laptop',
'price' => 431,
],
[
'prod_id' => 'def45678',
'name' => 'Karcher Hose Set',
'price' => 22,
],
],
],
[
'order_id' => 1197372932325,
'products' => [
[
'prod_id' => 'abc12345',
'name' => 'Asus Laptop',
'price' => 429,
],
],
],
[
'order_id' => 9812343774839,
'products' => [
[
'prod_id' => 'pqr88223',
'name' => 'Morphy Richards Food Mixer',
'price' => 431,
],
[
'prod_id' => 'def45678',
'name' => 'Karcher Hose Set',
'price' => 21,
],
],
],
[
'order_id' => 4433997244387,
'products' => [
[
'prod_id' => 'def45678',
'name' => 'Karcher Hose Set',
'price' => 23,
],
[
'prod_id' => 'jkl77336',
'name' => 'Picky Pencil Sharpener',
'price' => 1,
],
[
'prod_id' => 'xyz11228',
'name' => 'Russell Hobbs Chrome Kettle',
'price' => 16,
],
],
]
]
);Python
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new Python app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习本聚合教程之前,您必须设置一个新的Python应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install PyMongo and connect to MongoDB, see the Get Started with PyMongo tutorial.要了解如何安装PyMongo并连接到MongoDB,请参阅PyMongo入门教程。
To learn more about performing aggregations in PyMongo, see the Aggregation guide.要了解有关在PyMongo中执行聚合的更多信息,请参阅聚合指南。
After you install the library, create a file called 安装库后,创建一个名为agg_tutorial.py. Paste the following code in this file to create an app template for the aggregation tutorials.agg_tutorial.py的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
# Modify imports for each tutorial as needed.
from pymongo import MongoClient
# Replace the placeholder with your connection string.
uri = "<connection-string>"
client = MongoClient(uri)
try:
agg_db = client["agg_tutorials_db"]
# Get a reference to relevant collections.
# ... some_coll = agg_db["some_coll"]
# ... another_coll = agg_db["another_coll"]
# Delete any existing documents in collections if needed.
# ... some_coll.delete_many({})
# Insert sample data into the collection or collections.
# ... some_coll.insert_many(...)
# Create an empty pipeline array.
pipeline = []
# Add code to create pipeline stages.
# ... pipeline.append({...})
# Run the aggregation.
# ... aggregation_result = ...
# Print the aggregation results.
for document in aggregation_result:
print(document)
finally:
client.close()
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Get Started with the PHP Library tutorial.要了解如何定位部署的连接字符串,请参阅PHP库入门教程的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
uri = "mongodb+srv://mongodb-example:27017"Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
orders_coll = agg_db["orders"]
order_data = [
{
"order_id": 6363763262239,
"products": [
{
"prod_id": "abc12345",
"name": "Asus Laptop",
"price": 431,
},
{
"prod_id": "def45678",
"name": "Karcher Hose Set",
"price": 22,
},
],
},
{
"order_id": 1197372932325,
"products": [
{
"prod_id": "abc12345",
"name": "Asus Laptop",
"price": 429,
}
],
},
{
"order_id": 9812343774839,
"products": [
{
"prod_id": "pqr88223",
"name": "Morphy Richards Food Mixer",
"price": 431,
},
{
"prod_id": "def45678",
"name": "Karcher Hose Set",
"price": 21,
},
],
},
{
"order_id": 4433997244387,
"products": [
{
"prod_id": "def45678",
"name": "Karcher Hose Set",
"price": 23,
},
{
"prod_id": "jkl77336",
"name": "Picky Pencil Sharpener",
"price": 1,
},
{
"prod_id": "xyz11228",
"name": "Russell Hobbs Chrome Kettle",
"price": 16,
},
],
},
]
orders_coll.insert_many(order_data)Ruby
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new Ruby app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习本聚合教程之前,您必须设置一个新的Ruby应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the Ruby Driver and connect to MongoDB, see the Get Started with the Ruby Driver guide.要了解如何安装Ruby驱动程序并连接到MongoDB,请参阅Ruby驱动程序入门指南。
To learn more about performing aggregations in the Ruby Driver, see the Aggregation guide.要了解有关在Ruby驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为agg_tutorial.rb. Paste the following code in this file to create an app template for the aggregation tutorials.agg_tutorial.rb的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
# typed: strict
require 'mongo'
require 'bson'
# Replace the placeholder with your connection string.
uri = "<connection string>"
Mongo::Client.new(uri) do |client|
agg_db = client.use('agg_tutorials_db')
# Get a reference to relevant collections.
# ... some_coll = agg_db[:some_coll]
# Delete any existing documents in collections if needed.
# ... some_coll.delete_many({})
# Insert sample data into the collection or collections.
# ... some_coll.insert_many( ... )
# Add code to create pipeline stages within the array.
# ... pipeline = [ ... ]
# Run the aggregation.
# ... aggregation_result = some_coll.aggregate(pipeline)
# Print the aggregation results.
aggregation_result.each do |doc|
puts doc
end
end
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Ruby Get Started guide.要了解如何定位部署的连接字符串,请参阅Ruby入门指南的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
uri = "mongodb+srv://mongodb-example:27017"Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
orders = agg_db[:orders]
orders.delete_many({})
orders.insert_many(
[
{
order_id: 6363763262239,
products: [
{
prod_id: "abc12345",
name: "Asus Laptop",
price: 431,
},
{
prod_id: "def45678",
name: "Karcher Hose Set",
price: 22,
},
],
},
{
order_id: 1197372932325,
products: [
{
prod_id: "abc12345",
name: "Asus Laptop",
price: 429,
},
],
},
{
order_id: 9812343774839,
products: [
{
prod_id: "pqr88223",
name: "Morphy Richards Food Mixer",
price: 431,
},
{
prod_id: "def45678",
name: "Karcher Hose Set",
price: 21,
},
],
},
{
order_id: 4433997244387,
products: [
{
prod_id: "def45678",
name: "Karcher Hose Set",
price: 23,
},
{
prod_id: "jkl77336",
name: "Picky Pencil Sharpener",
price: 1,
},
{
prod_id: "xyz11228",
name: "Russell Hobbs Chrome Kettle",
price: 16,
},
],
},
]
)Rust
Create the Template App创建模板应用程序
Before you begin following this aggregation tutorial, you must set up a new Rust app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习本聚合教程之前,您必须设置一个新的Rust应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Rust Driver Quick Start guide.要了解如何安装驱动程序并连接到MongoDB,请参阅Rust驱动程序快速入门指南。
To learn more about performing aggregations in the Rust Driver, see the Aggregation guide.要了解有关在Rust驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为agg-tutorial.rs. Paste the following code in this file to create an app template for the aggregation tutorials.agg-tutorial.rs的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
use mongodb::{
bson::{doc, Document},
options::ClientOptions,
Client,
};
use futures::stream::TryStreamExt;
use std::error::Error;
// Define structs.
// #[derive(Debug, Serialize, Deserialize)]
// struct MyStruct { ... }
async fn main() mongodb::error::Result<()> {
// Replace the placeholder with your connection string.
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let agg_db = client.database("agg_tutorials_db");
// Get a reference to relevant collections.
// ... let some_coll: Collection<T> = agg_db.collection("...");
// ... let another_coll: Collection<T> = agg_db.collection("...");
// Delete any existing documents in collections if needed.
// ... some_coll.delete_many(doc! {}).await?;
// Insert sample data into the collection or collections.
// ... some_coll.insert_many(vec![...]).await?;
// Create an empty pipeline.
let mut pipeline = Vec::new();
// Add code to create pipeline stages.
// pipeline.push(doc! { ... });
// Run the aggregation and print the results.
let mut results = some_coll.aggregate(pipeline).await?;
while let Some(result) = results.try_next().await? {
println!("{:?}\n", result);
}
Ok(())
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Rust Quick Start guide.要了解如何定位部署的连接字符串,请参阅Rust快速入门指南的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
let uri = "mongodb+srv://mongodb-example:27017";Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
First, create Rust structs to model the data in the 首先,创建Rust结构体来对orders collection:orders集合中的数据进行建模:
struct Product {
prod_id: String,
name: String,
price: i32,
}
struct Order {
order_id: i64,
products: Vec<Product>,
}
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
let orders_coll: Collection<Order> = agg_db.collection("orders");
orders.delete_many(doc! {}).await?;
let orders = vec![
Order {
order_id: 6363763262239,
products: vec![
Product {
prod_id: "abc12345".to_string(),
name: "Asus Laptop".to_string(),
price: 431,
},
Product {
prod_id: "def45678".to_string(),
name: "Karcher Hose Set".to_string(),
price: 22,
},
],
},
Order {
order_id: 1197372932325,
products: vec![Product {
prod_id: "abc12345".to_string(),
name: "Asus Laptop".to_string(),
price: 429,
}],
},
Order {
order_id: 9812343774839,
products: vec![
Product {
prod_id: "pqr88223".to_string(),
name: "Morphy Richards Food Mixer".to_string(),
price: 431,
},
Product {
prod_id: "def45678".to_string(),
name: "Karcher Hose Set".to_string(),
price: 21,
},
],
},
Order {
order_id: 4433997244387,
products: vec![
Product {
prod_id: "def45678".to_string(),
name: "Karcher Hose Set".to_string(),
price: 23,
},
Product {
prod_id: "jkl77336".to_string(),
name: "Picky Pencil Sharpene".to_string(),
price: 1,
},
Product {
prod_id: "xyz11228".to_string(),
name: "Russell Hobbs Chrome Kettle".to_string(),
price: 16,
},
],
},
];
orders_coll.insert_many(orders).await?;scala
Create the Template App创建模板应用程序
Before you begin following an aggregation tutorial, you must set up a new Scala app. You can use this app to connect to a MongoDB deployment, insert sample data into MongoDB, and run the aggregation pipeline.在开始学习聚合教程之前,您必须设置一个新的Scala应用程序。您可以使用此应用程序连接到MongoDB部署,将示例数据插入MongoDB,并运行聚合管道。
Tip
To learn how to install the driver and connect to MongoDB, see the Get Started with the Scala Driver guide.要了解如何安装驱动程序并连接到MongoDB,请参阅Scala驱动程序入门指南。
To learn more about performing aggregations in the Scala Driver, see the Aggregation guide.要了解有关在Scala驱动程序中执行聚合的更多信息,请参阅聚合指南。
After you install the driver, create a file called 安装驱动程序后,创建一个名为AggTutorial.scala. Paste the following code in this file to create an app template for the aggregation tutorials.AggTutorial.scala的文件。将以下代码粘贴到此文件中,为聚合教程创建应用程序模板。
Important
In the following code, read the code comments to find the sections of the code that you must modify for the tutorial you are following.在下面的代码中,阅读代码注释,找到您必须为正在学习的教程修改的代码部分。
If you attempt to run the code without making any changes, you will encounter a connection error.如果您尝试在不进行任何更改的情况下运行代码,您将遇到连接错误。
package org.example;
// Modify imports for each tutorial as needed.
import org.mongodb.scala.MongoClient
import org.mongodb.scala.bson.Document
import org.mongodb.scala.model.{Accumulators, Aggregates, Field, Filters, Variable}
import java.text.SimpleDateFormat
object FilteredSubset {
def main(args: Array[String]): Unit = {
// Replace the placeholder with your connection string.
val uri = "<connection string>"
val mongoClient = MongoClient(uri)
Thread.sleep(1000)
val aggDB = mongoClient.getDatabase("agg_tutorials_db")
// Get a reference to relevant collections.
// ... val someColl = aggDB.getCollection("someColl")
// ... val anotherColl = aggDB.getCollection("anotherColl")
// Delete any existing documents in collections if needed.
// ... someColl.deleteMany(Filters.empty()).subscribe(...)
// If needed, create the date format template.
val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
// Insert sample data into the collection or collections.
// ... someColl.insertMany(...).subscribe(...)
Thread.sleep(1000)
// Add code to create pipeline stages within the Seq.
// ... val pipeline = Seq(...)
// Run the aggregation and print the results.
// ... someColl.aggregate(pipeline).subscribe(...)
Thread.sleep(1000)
mongoClient.close()
}
}
For every tutorial, you must replace the connection string placeholder with your deployment's connection string.对于每个教程,您必须将连接字符串占位符替换为部署的连接字符串。
Tip
To learn how to locate your deployment's connection string, see the Create a Connection String step of the Scala Driver Get Started guide.要了解如何定位部署的连接字符串,请参阅《Scala驱动程序入门》指南的创建连接字符串步骤。
For example, if your connection string is 例如,如果连接字符串是"mongodb+srv://mongodb-example:27017", your connection string assignment resembles the following:"mongodb+srv://mongodb-example:27017",连接字符串分配类似于以下内容:
val uri = "mongodb+srv://mongodb-example:27017"Create the Collection创建集合
This example uses an 此示例使用orders collection, which contains documents describing product orders. Because each order contains multiple products, the first step of the aggregation unpacks the products array into individual product order documents.orders集合,其中包含描述产品订单的文档。因为每个订单都包含多个产品,所以聚合的第一步将产品数组解包为单独的产品订单文档。
To create the 要创建orders collection and insert the sample data, add the following code to your application:orders集合并插入示例数据,请将以下代码添加到应用程序中:
val orders = aggDB.getCollection("orders")
orders.deleteMany(Filters.empty()).subscribe(
_ => {},
e => println("Error: " + e.getMessage),
)
orders.insertMany(Seq(
Document(
"order_id" -> 6363763262239L,
"products" -> Seq(
Document(
"prod_id" -> "abc12345",
"name" -> "Asus Laptop",
"price" -> 431
),
Document(
"prod_id" -> "def45678",
"name" -> "Karcher Hose Set",
"price" -> 22
)
)
),
Document(
"order_id" -> 1197372932325L,
"products" -> Seq(
Document(
"prod_id" -> "abc12345",
"name" -> "Asus Laptop",
"price" -> 429
)
)
),
Document(
"order_id" -> 9812343774839L,
"products" -> Seq(
Document(
"prod_id" -> "pqr88223",
"name" -> "Morphy Richards Food Mixer",
"price" -> 431
),
Document(
"prod_id" -> "def45678",
"name" -> "Karcher Hose Set",
"price" -> 21
)
)
),
Document(
"order_id" -> 4433997244387L,
"products" -> Seq(
Document(
"prod_id" -> "def45678",
"name" -> "Karcher Hose Set",
"price" -> 23
),
Document(
"prod_id" -> "jkl77336",
"name" -> "Picky Pencil Sharpener",
"price" -> 1
),
Document(
"prod_id" -> "xyz11228",
"name" -> "Russell Hobbs Chrome Kettle",
"price" -> 16
)
)
)
)).subscribe(
_ => {},
e => println("Error: " + e.getMessage),
)Steps步骤
The following steps demonstrate how to create and run an aggregation pipeline to unpack array fields into separate documents and compute new values based on groups of common values.以下步骤演示了如何创建和运行聚合管道,将数组字段解包到单独的文档中,并根据公共值组计算新值。
MongoDB Shell
Run the aggregation pipeline.运行聚合管道。
db.orders.aggregate( [
// Stage 1: Unwind the array of product orders
{ $unwind: { path: "$products" } },
// Stage 2: Match products that cost more than $15
{ $match: { "products.price": { $gt: 15 } } },
// Stage 3: Group products by product type
{ $group:
{
_id: "$products.prod_id",
product: { $first: "$products.name" },
total_value: { $sum: "$products.price" },
quantity: { $sum: 1 }
}
},
// Stage 4: Display the product ID
{ $set: { product_id: "$_id" } },
// Stage 5: Remove unneeded fields
{ $unset: [ "_id"] }
] )Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{
product: 'Asus Laptop',
total_value: 860,
quantity: 2,
product_id: 'abc12345'
}
{
product: 'Morphy Richards Food Mixer',
total_value: 431,
quantity: 1,
product_id: 'pqr88223'
}
{
product: 'Russell Hobbs Chrome Kettle',
total_value: 16,
quantity: 1,
product_id: 'xyz11228'
}
{
product: 'Karcher Hose Set',
total_value: 66,
quantity: 3,
product_id: 'def45678'
}
Note
If you run this example, the order of documents in your results might differ from the order of documents on this page because the aggregation pipeline does not contain a sort stage.如果运行此示例,则结果中的文档顺序可能与此页面上的文档顺序不同,因为聚合管道不包含排序阶段。
C
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
"{", "$unwind", "{", "path", BCON_UTF8("$products"), "}", "}",Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
"{", "$match", "{", "products.price", "{", "$gt", BCON_INT32(15), "}", "}", "}",Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
"{", "$group", "{",
"_id", BCON_UTF8("$products.prod_id"),
"product", "{", "$first", BCON_UTF8("$products.name"), "}",
"total_value", "{", "$sum", BCON_UTF8("$products.price"), "}",
"quantity", "{", "$sum", BCON_INT32(1), "}",
"}", "}",Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
"{", "$set", "{", "product_id", BCON_UTF8("$_id"), "}", "}",Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
"{", "$unset", "[", BCON_UTF8("_id"), "]", "}",Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
mongoc_cursor_t *results =
mongoc_collection_aggregate(orders, MONGOC_QUERY_NONE, pipeline, NULL, NULL);
bson_destroy(pipeline);
Ensure that you clean up the collection resources by adding the following line to your cleanup statements:通过在清理语句中添加以下行来确保清理集合资源:
mongoc_collection_destroy(orders);
Finally, run the following commands in your shell to generate and run the executable:最后,在shell中运行以下命令以生成并运行可执行文件:
gcc -o aggc agg-tutorial.c $(pkg-config --libs --cflags libmongoc-1.0)
./aggc
Tip
If you encounter connection errors by running the preceding commands in one call, you can run them separately.如果在一次调用中运行上述命令时遇到连接错误,可以单独运行它们。
Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{ "product" : "Asus Laptop", "total_value" : { "$numberInt" : "860" }, "quantity" : { "$numberInt" : "2" }, "product_id" : "abc12345" }
{ "product" : "Karcher Hose Set", "total_value" : { "$numberInt" : "66" }, "quantity" : { "$numberInt" : "3" }, "product_id" : "def45678" }
{ "product" : "Morphy Richards Food Mixer", "total_value" : { "$numberInt" : "431" }, "quantity" : { "$numberInt" : "1" }, "product_id" : "pqr88223" }
{ "product" : "Russell Hobbs Chrome Kettle", "total_value" : { "$numberInt" : "16" }, "quantity" : { "$numberInt" : "1" }, "product_id" : "xyz11228" }
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
C++11
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
pipeline.unwind("$products");Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
pipeline.match(bsoncxx::from_json(R"({
"products.price": { "$gt": 15 }
})"));Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
pipeline.group(bsoncxx::from_json(R"({
"_id": "$products.prod_id",
"product": { "$first": "$products.name" },
"total_value": { "$sum": "$products.price" },
"quantity": { "$sum": 1 }
})"));Add an addFields stage to display the product ID.添加addFields阶段以显示产品ID。
Add an 添加$addFields stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$addFields阶段,根据$group阶段设置的_id字段中的值重新创建product_id字段:
pipeline.add_fields(bsoncxx::from_json(R"({
"product_id": "$_id"
})"));Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
pipeline.append_stage(bsoncxx::from_json(R"({
"$unset": ["_id"]
})"));Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
auto cursor = orders.aggregate(pipeline);
Finally, run the following command in your shell to start your application:最后,在shell中运行以下命令以启动应用程序:
c++ --std=c++17 agg-tutorial.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
./app.outInterpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{ "product" : "Karcher Hose Set", "total_value" : 66, "quantity" : 3, "product_id" : "def45678" }
{ "product" : "Asus Laptop", "total_value" : 860, "quantity" : 2, "product_id" : "abc12345" }
{ "product" : "Morphy Richards Food Mixer", "total_value" : 431, "quantity" : 1, "product_id" : "pqr88223" }
{ "product" : "Russell Hobbs Chrome Kettle", "total_value" : 16, "quantity" : 1, "product_id" : "xyz11228" }
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
C#
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, start the aggregation on the 首先,启动orders collection and chain an $unwind stage to separate the entries in the Products array into individual documents:orders集合的聚合,并链接一个$unwind阶段,将Products数组中的条目分离到单独的文档中:
var results = orders.Aggregate()
.Unwind<Order, OrderUnwound>(o => o.Products)Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a Products.Price value greater than 15:$match阶段,用于匹配Products.Price值大于15的产品:
.Match(o => o.Products.Price > 15)Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加$group stage to collect order documents by the value of the ProductId field. $group阶段,根据ProductId字段的值集合订单文档。In this stage, add aggregation operations that create the following fields in the result documents:在此阶段,添加聚合操作,在结果文档中创建以下字段:
ProductId: the product ID (the grouping key):产品ID(分组键)Product: the product name:产品名称TotalValue: the total value of all the sales of the product:产品所有销售额的总价值Quantity: the number of orders for the product:产品的订单数量
.Group(
id: o => o.Products.ProductId,
group: g => new
{
ProductId = g.Key,
Product = g.First().Products.Name,
TotalValue = g.Sum(o => o.Products.Price),
Quantity = g.Count(),
}
);Run the aggregation and interpret the results.运行聚合并解释结果。
Finally, run the application in your IDE and inspect the results.最后,在IDE中运行应用程序并检查结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{ ProductId = pqr88223, Product = Morphy Richards Food Mixer, TotalValue = 431, Quantity = 1 }
{ ProductId = xyz11228, Product = Russell Hobbs Chrome Kettle, TotalValue = 16, Quantity = 1 }
{ ProductId = abc12345, Product = Asus Laptop, TotalValue = 860, Quantity = 2 }
{ ProductId = def45678, Product = Karcher Hose Set, TotalValue = 66, Quantity = 3 }
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Go
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
unwindStage := bson.D{{Key: "$unwind", Value: bson.D{
{Key: "path", Value: "$products"},
}}}Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
matchStage := bson.D{{Key: "$match", Value: bson.D{
{Key: "products.price", Value: bson.D{{Key: "$gt", Value: 15}}},
}}}Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
groupStage := bson.D{{Key: "$group", Value: bson.D{
{Key: "_id", Value: "$products.prod_id"},
{Key: "product", Value: bson.D{{Key: "$first", Value: "$products.name"}}},
{Key: "total_value", Value: bson.D{{Key: "$sum", Value: "$products.price"}}},
{Key: "quantity", Value: bson.D{{Key: "$sum", Value: 1}}},
}}}Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
setStage := bson.D{{Key: "$set", Value: bson.D{
{Key: "product_id", Value: "$_id"},
}}}Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
unsetStage := bson.D{{Key: "$unset", Value: bson.A{"_id"}}}Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
pipeline := mongo.Pipeline{
unwindStage,
matchStage,
groupStage,
setStage,
unsetStage,
}
cursor, err := orders.Aggregate(ctx, pipeline)
Finally, run the application and inspect the results.最后,运行应用程序并检查结果。
Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{"product":"Morphy Richards Food Mixer","total_value":431,"quantity":1,"product_id":"pqr88223"}
{"product":"Russell Hobbs Chrome Kettle","total_value":16,"quantity":1,"product_id":"xyz11228"}
{"product":"Karcher Hose Set","total_value":66,"quantity":3,"product_id":"def45678"}
{"product":"Asus Laptop","total_value":860,"quantity":2,"product_id":"abc12345"}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Java(Sync)
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
pipeline.add(Aggregates.unwind("$products"));Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
pipeline.add(Aggregates.match(
Filters.gt("products.price", 15)
));Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
pipeline.add(Aggregates.group(
"$products.prod_id",
Accumulators.first("product", "$products.name"),
Accumulators.sum("total_value", "$products.price"),
Accumulators.sum("quantity", 1)
));Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
pipeline.add(Aggregates.set(new Field<>("product_id", "$_id")));Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
pipeline.add(Aggregates.unset("_id"));Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
AggregateIterable<Document> aggregationResult = orders.aggregate(pipeline);
Finally, run the application in your IDE.最后,在IDE中运行应用程序。
Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{"product": "Asus Laptop", "total_value": 860, "quantity": 2, "product_id": "abc12345"}
{"product": "Russell Hobbs Chrome Kettle", "total_value": 16, "quantity": 1, "product_id": "xyz11228"}
{"product": "Karcher Hose Set", "total_value": 66, "quantity": 3, "product_id": "def45678"}
{"product": "Morphy Richards Food Mixer", "total_value": 431, "quantity": 1, "product_id": "pqr88223"}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Kotlin(Coroutine)
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
pipeline.add(
Aggregates.unwind("\$${Order::products.name}")
)Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
pipeline.add(
Aggregates.match(
Filters.gt("${Order::products.name}.${Product::price.name}", 15)
)
)Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prodID field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prodID字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
pipeline.add(
Aggregates.group(
"\$${Order::products.name}.${Product::prodID.name}",
Accumulators.first("product", "\$${Order::products.name}.${Product::name.name}"),
Accumulators.sum("total_value", "\$${Order::products.name}.${Product::price.name}"),
Accumulators.sum("quantity", 1)
)
)Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
pipeline.add(Aggregates.set(Field("product_id", "\$_id")))Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
pipeline.add(Aggregates.unset("_id"))Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
val aggregationResult = orders.aggregate<Document>(pipeline)
Finally, run the application in your IDE.最后,在IDE中运行应用程序。
Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
Document{{product=Russell Hobbs Chrome Kettle, total_value=16, quantity=1, product_id=xyz11228}}
Document{{product=Karcher Hose Set, total_value=66, quantity=3, product_id=def45678}}
Document{{product=Morphy Richards Food Mixer, total_value=431, quantity=1, product_id=pqr88223}}
Document{{product=Asus Laptop, total_value=860, quantity=2, product_id=abc12345}}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Node.js
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
pipeline.push({
$unwind: {
path: '$products',
},
});Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
pipeline.push({
$match: {
'products.price': {
$gt: 15,
},
},
});Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
pipeline.push({
$group: {
_id: '$products.prod_id',
product: { $first: '$products.name' },
total_value: { $sum: '$products.price' },
quantity: { $sum: 1 },
},
});Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
pipeline.push({
$set: {
product_id: '$_id',
},
});Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
pipeline.push({ $unset: ['_id'] });Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
const aggregationResult = await orders.aggregate(pipeline);
Finally, execute the code in the file using your IDE or the command line.最后,使用IDE或命令行执行文件中的代码。
Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{
product: 'Asus Laptop',
total_value: 860,
quantity: 2,
product_id: 'abc12345'
}
{
product: 'Morphy Richards Food Mixer',
total_value: 431,
quantity: 1,
product_id: 'pqr88223'
}
{
product: 'Russell Hobbs Chrome Kettle',
total_value: 16,
quantity: 1,
product_id: 'xyz11228'
}
{
product: 'Karcher Hose Set',
total_value: 66,
quantity: 3,
product_id: 'def45678'
}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
PHP
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
Stage::unwind(
path: Expression::arrayFieldPath('products')
),Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
Stage::match(
['products.price' => Query::gt(15)]
),Add a group stage to group by product type.按产品类型添加分组阶段。
Outside of your 在Pipeline instance, create a $group stage in a factory function to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:Pipeline实例之外,在工厂函数中创建一个$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
function groupByProductStage()
{
return Stage::group(
_id: Expression::stringFieldPath('products.prod_id'),
product: Accumulator::first(
Expression::stringFieldPath('products.name')
),
total_value: Accumulator::sum(
Expression::numberFieldPath('products.price'),
),
quantity: Accumulator::sum(1)
);
}
Then, in your 然后,在Pipeline instance, call the groupByProductStage() function:Pipeline实例中,调用groupByProductStage()函数:
groupByProductStage(),Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
Stage::set(product_id: Expression::stringFieldPath('_id')),Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
Stage::unset('_id')Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
$cursor = $orders->aggregate($pipeline);
Finally, run the following command in your shell to start your application:最后,在shell中运行以下命令以启动应用程序:
php agg_tutorial.phpInterpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{
"product": "Russell Hobbs Chrome Kettle",
"total_value": 16,
"quantity": 1,
"product_id": "xyz11228"
}
{
"product": "Asus Laptop",
"total_value": 860,
"quantity": 2,
"product_id": "abc12345"
}
{
"product": "Karcher Hose Set",
"total_value": 66,
"quantity": 3,
"product_id": "def45678"
}
{
"product": "Morphy Richards Food Mixer",
"total_value": 431,
"quantity": 1,
"product_id": "pqr88223"
}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Python
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
pipeline.append({"$unwind": {"path": "$products"}})Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
pipeline.append({"$match": {"products.price": {"$gt": 15}}})Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
pipeline.append(
{
"$group": {
"_id": "$products.prod_id",
"product": {"$first": "$products.name"},
"total_value": {"$sum": "$products.price"},
"quantity": {"$sum": 1},
}
}
)Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
pipeline.append({"$set": {"product_id": "$_id"}})Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
pipeline.append({"$unset": ["_id"]})Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
aggregation_result = orders_coll.aggregate(pipeline)
Finally, run the following command in your shell to start your application:最后,在shell中运行以下命令以启动应用程序:
python3 agg_tutorial.pyInterpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{'product': 'Asus Laptop', 'total_value': 860, 'quantity': 2, 'product_id': 'abc12345'}
{'product': 'Karcher Hose Set', 'total_value': 66, 'quantity': 3, 'product_id': 'def45678'}
{'product': 'Morphy Richards Food Mixer', 'total_value': 431, 'quantity': 1, 'product_id': 'pqr88223'}
{'product': 'Russell Hobbs Chrome Kettle', 'total_value': 16, 'quantity': 1, 'product_id': 'xyz11228'}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Ruby
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
{
"$unwind": {
path: "$products",
},
},Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将productsprice值大于15的产品进行匹配:
{
"$match": {
"products.price": {
"$gt": 15,
},
},
},Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
{
"$group": {
_id: "$products.prod_id",
product: { "$first": "$products.name" },
total_value: { "$sum": "$products.price" },
quantity: { "$sum": 1 },
},
},Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
{
"$set": {
product_id: "$_id",
},
},Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
{ "$unset": ["_id"] },Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
aggregation_result = orders.aggregate(pipeline)
Finally, run the following command in your shell to start your application:最后,在shell中运行以下命令以启动应用程序:
ruby agg_tutorial.rbInterpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{"product"=>"Asus Laptop", "total_value"=>860, "quantity"=>2, "product_id"=>"abc12345"}
{"product"=>"Russell Hobbs Chrome Kettle", "total_value"=>16, "quantity"=>1, "product_id"=>"xyz11228"}
{"product"=>"Karcher Hose Set", "total_value"=>66, "quantity"=>3, "product_id"=>"def45678"}
{"product"=>"Morphy Richards Food Mixer", "total_value"=>431, "quantity"=>1, "product_id"=>"pqr88223"}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
Rust
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
pipeline.push(doc! {
"$unwind": {
"path": "$products"
}
});Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
pipeline.push(doc! {
"$match": {
"products.price": { "$gt": 15 }
}
});Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
pipeline.push(doc! {
"$group": {
"_id": "$products.prod_id",
"product": { "$first": "$products.name" },
"total_value": { "$sum": "$products.price" },
"quantity": { "$sum": 1 }
}
});Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the prod_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建prod_id字段:
pipeline.push(doc! {
"$set": {
"prod_id": "$_id"
}
});Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
pipeline.push(doc! {
"$unset": ["_id"]
});Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
let mut cursor = orders_coll.aggregate(pipeline).await?;
Finally, run the following command in your shell to start your application:最后,在shell中运行以下命令以启动应用程序:
cargo runInterpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
Document({"product": String("Russell Hobbs Chrome Kettle"), "total_value": Int32(16), "quantity": Int32(1),
"prod_id": String("xyz11228")})
Document({"product": String("Morphy Richards Food Mixer"), "total_value": Int32(431), "quantity": Int32(1),
"prod_id": String("pqr88223")})
Document({"product": String("Karcher Hose Set"), "total_value": Int32(66), "quantity": Int32(3),
"prod_id": String("def45678")})
Document({"product": String("Asus Laptop"), "total_value": Int32(860), "quantity": Int32(2),
"prod_id": String("abc12345")})
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。
scala
Add an unwind stage to unpack the array of product orders.添加一个展开阶段来打开一系列产品订单。
First, add an 首先,添加一个$unwind stage to separate the entries in the products array into individual documents:$unwind阶段,将products数组中的条目分隔成单独的文档:
Aggregates.unwind("$products"),Add a match stage for products that cost more than $15.为价格超过15美元的产品添加匹配阶段。
Next, add a 接下来,添加一个$match stage that matches products with a products.price value greater than 15:$match阶段,将products.price值大于15的产品进行匹配:
Aggregates.filter(Filters.gt("products.price", 15)),Add a group stage to group by product type.按产品类型添加分组阶段。
Add a 添加一个$group stage to collect order documents by the value of the prod_id field. In this stage, add aggregation operations that create the following fields in the result documents:$group阶段,根据prod_id字段的值集合订单文档。在此阶段,添加聚合操作,在结果文档中创建以下字段:
product: the product name:产品名称total_value: the total value of all the sales of the product:产品所有销售额的总价值quantity: the number of orders for the product:产品的订单数量
Aggregates.group(
"$products.prod_id",
Accumulators.first("product", "$products.name"),
Accumulators.sum("total_value", "$products.price"),
Accumulators.sum("quantity", 1)
),Add a set stage to display the product ID.添加一个设置阶段来显示产品ID。
Add a 添加一个$set stage to recreate the product_id field from the values in the _id field that were set during the $group stage:$set阶段,根据在$group阶段设置的_id字段中的值重新创建product_id字段:
Aggregates.set(Field("product_id", "$_id")),Add an unset stage to remove unneeded fields.添加未设置的阶段以删除不需要的字段。
Finally, add an 最后,添加一个$unset stage. The $unset stage removes the _id field from the result documents:$unset阶段。$unset阶段从结果文档中删除_id字段:
Aggregates.unset("_id")Run the aggregation pipeline.运行聚合管道。
Add the following code to the end of your application to perform the aggregation on the 将以下代码添加到应用程序的末尾,以对orders collection:orders集合执行聚合:
orders.aggregate(pipeline)
.subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"Error: $e"))
Finally, run the application in your IDE.最后,在IDE中运行应用程序。
Interpret the aggregation results.解释聚合结果。
The aggregation returns the following summary of customers' orders from 2020:该汇总返回了2020年以来客户订单的以下摘要:
{"product": "Morphy Richards Food Mixer", "total_value": 431, "quantity": 1, "product_id": "pqr88223"}
{"product": "Karcher Hose Set", "total_value": 66, "quantity": 3, "product_id": "def45678"}
{"product": "Russell Hobbs Chrome Kettle", "total_value": 16, "quantity": 1, "product_id": "xyz11228"}
{"product": "Asus Laptop", "total_value": 860, "quantity": 2, "product_id": "abc12345"}
The result documents contain details about the total value and quantity of orders for products that cost more than $15.结果文档包含成本超过15美元的产品订单的总价值和数量的详细信息。