On this page本页内容
MongoDB for VS Code is currently available as a Preview in the Visual Studio Marketplace. The product, its features, and the corresponding documentation may change during the Preview stage.
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:
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:
In Visual Studio Code, press one of the following key combinations:
The Command Palette provides quick access to commands and keyboard shortcuts.
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 playground pre-configured with a few commands.
You can load new Playgrounds without the template by disabling 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 outputs the results of your playground to the Output view in Visual Studio Code.
To use 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:
test
database.test.sales
collection.When you press the Play Button, this operation outputs the following documents to the Output view in Visual Studio Code:
[
{
_id: 'abc',
totalSaleAmount: 120
},
{
_id: 'jkl',
totalSaleAmount: 20
},
{
_id: 'xyz',
totalSaleAmount: 150
}
]