Run Aggregation Pipelines运行聚合管道
On this page本页内容
You can run aggregation pipelines on your collections using the MongoDB Shell. 您可以使用MongoDB Shell在集合上运行聚合管道。Aggregation pipelines transform your documents into aggregated results based on selected pipeline stages.聚合管道根据选定的管道阶段将文档转换为聚合结果。
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 Shell outputs the results directly to the terminal.当您运行聚合时,MongoDB Shell会将结果直接输出到终端。
Understand the Aggregation Syntax了解聚合语法
The MongoDB aggregation pipeline consists of stages. MongoDB聚合管道由几个阶段组成。Each stage transforms the documents as they pass through the pipeline. 每个阶段都会在文档通过管道时对其进行转换。Pipeline stages do not need to produce one output document for every input document; e.g., some stages may generate new documents or filter out documents.管道阶段不需要为每个输入文档生成一个输出文档;例如,某些阶段可能会生成新文档或筛选掉文档。
To create an aggregation pipeline, use the following syntax in the MongoDB Shell:要创建聚合管道,请在MongoDB Shell中使用以下语法:
db.<collection>.aggregate([
{
<$stage1>
},
{
<$stage2>
}
...
])
Example实例
The examples on this page reference the Atlas sample dataset. 本页的示例引用了Atlas示例数据集。You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. 您可以创建一个免费的Atlas集群,并使用示例数据填充该集群,以配合这些示例。To learn more, see Get Started with Atlas.要了解更多信息,请参阅Atlas入门。
The example below uses the 下面的示例使用Atlas sample_mflix示例数据集中的movies
collection in the Atlas sample_mflix sample dataset.movies
集合。
Example Document示例文档
Each document in the movies
collection describes a movie:movies
集合中的每个文档都描述了一部电影:
{
_id: 573a1397f29313caabce8347,
fullplot: 'In a cyberpunk vision of the future, man has developed the technology to create replicants, human clones used to serve in the colonies outside Earth but with fixed lifespans. In Los Angeles, 2019, Deckard is a Blade Runner, a cop who specializes in terminating replicants. Originally in retirement, he is forced to re-enter the force when four replicants escape from an off-world colony to Earth.',
imdb: { rating: 8.2, votes: 419589, id: 83658 },
year: 1982,
plot: 'A blade runner must pursue and try to terminate four replicants who stole a ship in space and have returned to Earth to find their creator.',
genres: [ 'Sci-Fi', 'Thriller' ],
rated: 'R',
metacritic: 88,
title: 'Blade Runner',
lastupdated: '2015-09-04 00:05:51.990000000',
languages: [ 'English', 'German', 'Cantonese', 'Japanese', 'Hungarian' ],
writers: [
'Hampton Fancher (screenplay)',
'David Webb Peoples (screenplay)',
'Philip K. Dick (novel)'
],
type: 'movie',
tomatoes: {
viewer: { rating: 4, numReviews: 331213, meter: 91 },
dvd: 1997-08-27T00:00:00.000Z,
critic: { rating: 8.5, numReviews: 102, meter: 90 },
lastUpdated: 2015-09-12T17:48:21.000Z,
consensus: "Misunderstood when it first hit theaters, the influence of Ridley Scott's mysterious, neo-noir Blade Runner has deepened with time. A visually remarkable, achingly human sci-fi masterpiece.",
rotten: 10,
production: 'Warner Bros. Pictures',
fresh: 92
},
poster: 'https://m.media-amazon.com/images/M/MV5BNzQzMzJhZTEtOWM4NS00MTdhLTg0YjgtMjM4MDRkZjUwZDBlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SY1000_SX677_AL_.jpg',
num_mflix_comments: 1,
released: 1982-06-25T00:00:00.000Z,
awards: {
wins: 13,
nominations: 15,
text: 'Nominated for 2 Oscars. Another 11 wins & 15 nominations.'
},
countries: [ 'USA', 'Hong Kong', 'UK' ],
cast: [
'Harrison Ford',
'Rutger Hauer',
'Sean Young',
'Edward James Olmos'
],
directors: [ 'Ridley Scott' ],
runtime: 117
}
The documents aggregated in this tutorial reside in the 本教程中聚合的文档位于sample_mflix.movies
collection. sample_mflix.movies
集合中。Use the following command to switch to the database that contains this collection:使用以下命令切换到包含此集合的数据库:
use sample_mflix
Example Aggregation Pipeline聚合管道示例
Consider the following pipeline:考虑以下管道:
db.movies.aggregate([
// First Stage
{ $project: { _id: 0, genres: 1, imdb: 1, title: 1 } },
// Second Stage
{ $unwind: "$genres" },
// Third Stage
{ $group:
{ _id: "$genres",
averageGenreRating: { $avg: "$imdb.rating" }
}
},
// Fourth Stage
{ $sort: { averageGenreRating: -1 } }
] )
This pipeline performs an aggregation in four stages:此管道分四个阶段执行聚合:
First Stage第一阶段-
The$project
stage passes documents that contain the following fields to the next pipeline stage:$project
阶段将包含以下字段的文档传递到下一个管道阶段:genres
imdb
title
Documents from the collection that don't include all of these fields are not passed to the next pipeline stage.集合中不包括所有这些字段的文档不会传递到下一个管道阶段。NoteThe$project
stage specifies_id: 0
to suppress the_id
field from the documents it passes to the next stage.$project
阶段指定_id:0
以从传递到下一阶段的文档中取消显示_id
字段。For more information, see the MongoDB Manual.有关更多信息,请参阅MongoDB手册。The$project
stage transforms the example document and passes the following output to the next pipeline stage:$project
阶段转换示例文档,并将以下输出传递到下一个管道阶段:{
imdb: { rating: 8.2, votes: 419589, id: 83658 },
genres: [ 'Sci-Fi', 'Thriller' ],
title: 'Blade Runner'
}Second Stage第二阶段The$unwind
stage passes a document for each element in thegenres
array to the next pipeline stage.$unwind
阶段将genres
数组中每个元素的文档传递到下一个管道阶段。The$unwind
stage generates the following two documents from the original example document, then passes them to the next pipeline stage:$unwind
阶段从原始示例文档生成以下两个文档,然后将它们传递到下一个管道阶段:{
imdb: { rating: 8.2, votes: 419589, id: 83658 },
genres: 'Sci-Fi',
title: 'Blade Runner'
}{
imdb: { rating: 8.2, votes: 419589, id: 83658 },
genres: 'Thriller',
title: 'Blade Runner'
}Third Stage第三阶段Retrieves the distinct genre values from the documents it receives from the previous pipeline stage,从它从前一管道阶段接收的文档中检索不同的类型值,Creates a document for each distinct genre where the为每个不同类型创建文档,其中_id
is the genre name,_id
是类型名称,Adds a field,为每个新文档添加一个字段averageGenreRating
, to each new document that contains the averageimdb.rating
of all documents that match the genre, andaverageGenreRating
,该字段包含与类型匹配的所有文档的平均imdb.rating
,以及Passes the new documents to the next pipeline stage.将新文档传递到下一个管道阶段。
This stage sends documents that look similar to the following to the next pipeline stage:此阶段将类似以下内容的文档发送到下一个管道阶段:{ _id: 'Sport', averageGenreRating: 6.781233933161954 },
{ _id: 'History', averageGenreRating: 7.202306920762287 },
{ _id: 'Biography', averageGenreRating: 7.097142857142857 },
{ _id: 'Adventure', averageGenreRating: 6.527788649706458 },
{ _id: 'Family', averageGenreRating: 6.36096256684492 },
{ _id: 'Crime', averageGenreRating: 6.730478683620045 },
{ _id: 'Western', averageGenreRating: 6.879197080291971 },
{ _id: 'Fantasy', averageGenreRating: 6.42495652173913 },
{ _id: 'Talk-Show', averageGenreRating: 7 },
{ _id: 'Documentary', averageGenreRating: 7.365266635205286 },
{ _id: 'War', averageGenreRating: 7.183944374209861 },
{ _id: 'Short', averageGenreRating: 7.355813953488372 },
{ _id: 'Horror', averageGenreRating: 5.84110718492344 },
{ _id: 'Film-Noir', averageGenreRating: 7.503809523809523 },
{ _id: 'News', averageGenreRating: 7.254901960784314 },
{ _id: 'Thriller', averageGenreRating: 6.322121555303888 },
{ _id: 'Action', averageGenreRating: 6.3774842271293375 },
{ _id: 'Music', averageGenreRating: 6.923452380952381 },
{ _id: 'Animation', averageGenreRating: 6.917993795243019 },
{ _id: 'Drama', averageGenreRating: 6.830528688822631 }Fourth Stage第四阶段The$sort
stage sorts the documents it receives from the previous stage in descending order based on the value of theaverageGenreRating
field.$sort
阶段根据averageGenreRating
字段的值,按降序对从上一阶段接收的文档进行排序。When you run the example pipeline, the MongoDB Shell prints documents similar to the following to the terminal:当您运行示例管道时,MongoDB Shell会将类似以下内容的文档打印到终端:[
{ _id: 'Film-Noir', averageGenreRating: 7.503809523809523 },
{ _id: 'Documentary', averageGenreRating: 7.365266635205286 },
{ _id: 'Short', averageGenreRating: 7.355813953488372 },
{ _id: 'News', averageGenreRating: 7.254901960784314 },
{ _id: 'History', averageGenreRating: 7.202306920762287 },
{ _id: 'War', averageGenreRating: 7.183944374209861 },
{ _id: 'Biography', averageGenreRating: 7.097142857142857 },
{ _id: 'Talk-Show', averageGenreRating: 7 },
{ _id: 'Music', averageGenreRating: 6.923452380952381 },
{ _id: 'Animation', averageGenreRating: 6.917993795243019 },
{ _id: 'Western', averageGenreRating: 6.879197080291971 },
{ _id: 'Drama', averageGenreRating: 6.830528688822631 },
{ _id: 'Sport', averageGenreRating: 6.781233933161954 },
{ _id: 'Crime', averageGenreRating: 6.730478683620045 },
{ _id: 'Musical', averageGenreRating: 6.696913580246913 },
{ _id: 'Romance', averageGenreRating: 6.695711554220159 },
{ _id: 'Mystery', averageGenreRating: 6.563317384370015 },
{ _id: 'Adventure', averageGenreRating: 6.527788649706458 },
{ _id: 'Comedy', averageGenreRating: 6.479626461362988 },
{ _id: 'Fantasy', averageGenreRating: 6.42495652173913 }
]TipSee 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.要了解有关阶段中可用聚合运算符的更多信息,请参阅聚合管道运算符。