Docs Menu

Docs HomeDevelop ApplicationsMongoDB for VS Code

Run Aggregation Pipelines

On this page

  • Open a Playground to Run Aggregation Pipelines
  • Open the Visual Studio Code Command Palette.
  • Find and run the "Create MongoDB Playground" command.
  • Create and Run an Aggregation Pipeline
  • Example

You can run aggregation pipelines on your collections in 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.

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

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

Use the Command Palette search bar to search for commands. All commands related to MongoDB for VS Code are prefaced with 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.

Note

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.

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. If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.

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:

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 stage filters the data such that only sales from the year 2014 are passed to the next stage.
    Second Stage
    The $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, MongoDB for VS Code splits your playground and outputs the following document in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab:

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

Tip

See also:

←  Delete DocumentsExport a Query or Pipeline to Language →