> This page location: /docs.md > /docs/tools-and-connectors.md > /docs/mongodb-vscode.md > /docs/mongodb-vscode/playgrounds.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Run Aggregation Pipelines with VS Code

You can run [aggregation pipelines](https://www.mongodb.com/docs/manual/aggregation.md#std-label-aggregation) on your collections in VS Code Extension. Aggregation pipelines consist of [stages](https://www.mongodb.com/docs/manual/reference/mql/aggregation-stages.md#std-label-aggregation-pipeline-operator-reference) 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, VS Code Extension conveniently outputs the results directly within 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.

To open a new MongoDB Playground:

1. Open the Visual Studio Code Command Palette.

   In Visual Studio Code, press one of the following key combinations:

   - Control + Shift + P on Windows or Linux.

   - Command + Shift + P on macOS.

   The Command Palette provides quick access to commands and keyboard shortcuts.

2. Find and run the "Create MongoDB Playground" command.

   Use the Command Palette search bar to search for commands. All commands related to VS Code Extension are prefaced with MongoDB:.

   When you run the MongoDB: Create MongoDB Playground command, VS Code Extension opens a default playground template pre-configured with a few commands.

   **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.](https://www.mongodb.com/docs/mongodb-vscode/settings.md#std-label-vsce-settings)

## Create and Run an Aggregation Pipeline

To create an aggregation pipeline, use the following syntax in your Playground:

```text
db.<collection>.aggregate([
  {
    <$stage1>
  },
  {
    <$stage2>
  }
  ...
])
```

To run your Playground, press the  button at the top right corner of the Playground View. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground
Results pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.

### Example

To run this example, **start with a blank MongoDB Playground** by clearing the template Playground if it is loaded.

Consider the following playground which inserts sample data into a collection and aggregates that data:

```javascript
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:

1. Switches to the `test` database.

2. Inserts eight documents into the `test.sales` collection.

3. Performs an aggregation in two stages:

   First Stage

   The [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) stage filters the data such that only sales from the year 2014 are passed to the next stage.

   Second Stage

   The [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage groups the data by item. The stage adds a new field to the output called `totalSaleAmount`, which is the culmination of the item's `price` and `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:

```javascript
[
  {
    _id: 'abc',
    totalSaleAmount: 120
  },
  {
    _id: 'jkl',
    totalSaleAmount: 20
  },
  {
    _id: 'xyz',
    totalSaleAmount: 150
  }
]
```

**See also:**

- To learn more about the available aggregation stages, see [Aggregation Stages.](https://www.mongodb.com/docs/manual/reference/mql/aggregation-stages.md#std-label-aggregation-pipeline-operator-reference)

- To learn more about the available aggregation operators you can use within stages, see [Expressions.](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-pipeline-operators)
