Docs Home → MongoDB for VS Code
Run Aggregation Pipelines运行聚合管道
On this page本页内容
You can run aggregation pipelines on your collections in MongoDB for VS Code. 您可以在MongoDB for VS Code中对集合运行聚合管道。Aggregation pipelines consist of stages that process your data and return computed results.聚合管道由处理数据并返回计算结果的阶段组成。
Common uses for aggregation include:聚合的常见用途包括:
Grouping data by a given expression.按给定表达式对数据进行分组。Calculating results based on multiple fields and storing those results in a new field.基于多个字段计算结果,并将这些结果存储在新字段中。Filtering data to return a subset that matches a given criteria.筛选数据以返回与给定条件匹配的子集。Sorting data.排序数据。
When you run an aggregation, MongoDB for VS Code conveniently outputs the results directly within Visual Studio Code.当您运行聚合时,MongoDB for VS Code可以方便地直接在Visual Studio Code中输出结果。
Open a Playground to Run Aggregation Pipelines打开演练场以运行聚合管道
You can run aggregation pipelines in a MongoDB Playground. MongoDB Playgrounds are JavaScript environments where you can prototype queries, aggregations, and MongoDB commands with helpful syntax highlighting.您可以在MongoDB Playground中运行聚合管道。MongoDB Playgrounds是JavaScript环境,在这里您可以通过有用的语法高亮显示来原型查询、聚合和MongoDB命令。
To open a new MongoDB Playground:要打开一个新的MongoDB演练场:
Open the Visual Studio Code Command Palette.打开Visual Studio Code“命令面板”。
In Visual Studio Code, press one of the following key combinations:在Visual Studio Code中,按以下组合键之一:
- Control + Shift + P on Windows or Linux.
- Command + Shift + P on macOS.
The Command Palette provides quick access to commands and keyboard shortcuts.“命令面板”提供了对命令和键盘快捷键的快速访问。
Find and run the "Create MongoDB Playground" command.找到并运行“创建MongoDB演练场”命令。
Use the Command Palette search bar to search for commands. 使用“命令面板”搜索栏可以搜索命令。All commands related to MongoDB for VS Code are prefaced with MongoDB:.所有与MongoDB for VS Code相关的命令都以MongoDB:
开头。
When you run the MongoDB: Create MongoDB Playground command, MongoDB for VS Code opens a default playground template pre-configured with a few commands.当您运行“MongoDB:创建MongoDB演练场”命令时,MongoDB for VS Code会打开一个默认的演练场模板,该模板预先配置了一些命令。
To load new Playgrounds without the template, disable the Use Default Template For Playground setting. 若要加载不带模板的新演练场,请禁用“为演练场使用默认模板”设置。To learn more about MongoDB for VS Code settings, see MongoDB for VS Code Settings.要了解有关MongoDB for VS Code设置的更多信息,请参阅MongoDB for VSCode设置。
Create and Run an Aggregation Pipeline创建并运行聚合管道
To create an aggregation pipeline, use the following syntax in your Playground:要创建聚合管道,请在演练场中使用以下语法:
db.<collection>.aggregate([
{
<$stage1>
},
{
<$stage2>
}
...
])
To run your Playground, press the Play Button at the top right of the Playground View. 要运行您的演练场,请按演练场视图右上角的“播放”按钮。MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. MongoDB for VS Code拆分您的演练场,并在Playground Results.json
窗格中输出演练场的结果。If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出您的演练场的结果。
Example实例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB Playground开始,如果加载了模板Playground,请清除该模板。
Consider the following playground which inserts sample data into a collection and aggregates that data:考虑以下演练场,它将样本数据插入集合并聚合这些数据:
use("test");
db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z") },
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : new Date("2014-03-01T09:00:00Z") },
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : new Date("2014-03-15T09:00:00Z") },
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : new Date("2014-04-04T11:21:39.736Z") },
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : new Date("2014-04-04T21:23:13.331Z") },
{ "_id" : 6, "item" : "def", "price" : 7.5, "quantity": 5, "date" : new Date("2015-06-04T05:08:13Z") },
{ "_id" : 7, "item" : "def", "price" : 7.5, "quantity": 10, "date" : new Date("2015-09-10T08:43:00Z") },
{ "_id" : 8, "item" : "abc", "price" : 10, "quantity" : 5, "date" : new Date("2016-02-06T20:20:13Z") }
])
db.sales.aggregate([
{ $match: { date: { $gte: new Date("2014-01-01"), $lt: new Date("2015-01-01") } } },
{ $group: { _id: "$item", totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } } } }
])
This pipeline:此管道:
Switches to the切换到test
database.test
数据库。Inserts eight documents into the在test.sales
collection.test.sales
集合中插入八个文档。Performs an aggregation in two stages:分两个阶段执行聚合:First Stage第一阶段The$match
stage filters the data such that only sales from the year 2014 are passed to the next stage.$match
阶段筛选数据,以便仅将2014年的销售额传递到下一阶段。Second Stage第二阶段The$group
stage groups the data by item.$group
阶段按项目对数据进行分组。The stage adds a new field to the output called该阶段在输出中添加一个名为totalSaleAmount
, which is the culmination of the item'sprice
andquantity
.totalSaleAmount
的新字段,该字段是商品price
和quantity
的顶点。
When you press the Play Button, MongoDB for VS Code splits your playground and outputs the following document in the Playground Results.json pane. 当您按下“播放”按钮时,MongoDB for VS Code会分割您的演练场,并在Playground Results.json
窗格中输出以下文档。If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab:如果您禁用了拆分视图,MongoDB for VS Code会在一个新的选项卡中输出以下文档:
[
{
_id: 'abc',
totalSaleAmount: 120
},
{
_id: 'jkl',
totalSaleAmount: 20
},
{
_id: 'xyz',
totalSaleAmount: 150
}
]
See also:
To learn more about the available aggregation stages, see Aggregation Pipeline Stages.要了解有关可用聚合阶段的更多信息,请参阅聚合管道阶段。To learn more about the available aggregation operators you can use within stages, see Aggregation Pipeline Operators.要了解有关阶段中可用聚合运算符的更多信息,请参阅聚合管道运算符。