Run Aggregation Pipelines
On this page
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.
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:
Find and run the "Create MongoDB Playground" command.
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.
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. If you disabled split-view, MongoDB for VS Code 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:
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.Inserts eight documents into the
test.sales
collection.Performs an aggregation in two stages:
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:
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.