You can run aggregation pipelines on your collections in VS Code Extension. Aggregation pipelines consist of stages that process your data and return computed results.您可以在VS Code扩展中对集合运行聚合管道。聚合管道由处理数据并返回计算结果的阶段组成。
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, VS Code Extension conveniently outputs the results directly within Visual Studio Code.当您运行聚合时,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游乐场中运行聚合管道。MongoDB游乐场是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.Windows或Linux上的Control+Shift+P。Command + Shift + P on macOS.macOS上的Command+Shift+P。
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 VS Code Extension are prefaced with MongoDB:.使用“命令选项板”搜索栏搜索命令。所有与VS Code扩展相关的命令都以MongoDB开头:。
When you run the MongoDB: Create MongoDB Playground command, VS Code Extension opens a default playground template pre-configured with a few commands.当您运行“MongoDB:创建MongoDB游乐场”命令时,VS Code扩展会打开一个预先配置了几个命令的默认游乐场模板。
Note
To load new Playgrounds without the template, disable the Use Default Template For Playground setting. 要加载没有模板的新游乐场,请禁用“使用默认游乐场模板”设置。To learn more about VS Code Extension settings, see Visual Studio Code Settings.要了解有关VS Code扩展设置的更多信息,请参阅Visual Studio Code设置。
Create and Run an Aggregation Pipeline创建并运行聚合管道
To create an aggregation pipeline, use the following syntax in your Playground:要创建聚合管道,请在Playground中使用以下语法:
db.<collection>.aggregate([
{
<$stage1>
},
{
<$stage2>
}
...
])
To run your Playground, press the Play Button at the top right of the Playground View. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. 要运行游乐场,请按游乐场视图右上角的“播放”按钮。VS Code扩展拆分游乐场,并在“游乐场Results.json”窗格中输出游乐场的结果。If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.如果禁用拆分视图,VS Code扩展将在新选项卡中输出游乐场的结果。
Example示例
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.要运行此示例,请从一个空白的MongoDB游乐场开始,如果加载了模板游乐场,请清除它。
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切换到testdatabase.test数据库。Inserts eight documents into the将八个文档插入到test.salescollection.test.sales集合中。Performs an aggregation in two stages:分两个阶段执行聚合:First Stage第一阶段The$matchstage filters the data such that only sales from the year 2014 are passed to the next stage.$match阶段筛选数据,以便只有2014年的销售额才能传递到下一阶段。Second Stage第二阶段The$groupstage groups the data by item.$group阶段按项目对数据进行分组。The stage adds a new field to the output called该阶段在输出中添加了一个名为totalSaleAmount, which is the culmination of the item'spriceandquantity.totalSaleAmount的新字段,这是商品price和quantity的最高值。
When you press the Play Button, VS Code Extension splits your playground and outputs the following document in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the following document in a new tab:当您按下“播放”按钮时,VS Code扩展会拆分游乐场,并在“游乐场Results.json”窗格中输出以下文档。如果禁用拆分视图,VS Code扩展将在新选项卡中输出以下文档:
[
{
_id: 'abc',
totalSaleAmount: 120
},
{
_id: 'jkl',
totalSaleAmount: 20
},
{
_id: 'xyz',
totalSaleAmount: 150
}
]
Tip
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.要了解有关可以在阶段中使用的可用聚合运算符的更多信息,请参阅聚合管道运算符。