In the 在db.collection.aggregate()
method and db.aggregate()
method, pipeline stages appear in an array. db.collection.aggregate()
方法和db.aggregate()
方法中,管道阶段以数组的形式出现。In the Atlas UI, you can arrange pipeline stages using the aggregation pipeline builder. 在Atlas UI中,您可以使用聚合管道生成器来排列管道阶段。Documents pass through the stages in sequence.文件按顺序通过各个阶段。
Compatibility兼容性
You can use pipeline stages for deployments hosted in the following environments:您可以将管道阶段用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
db.collection.aggregate()
Stages阶段
All stages except the 除$out
, $merge
, $geoNear
, $changeStream
, and $changeStreamSplitLargeEvent
stages can appear multiple times in a pipeline.$out
、$merge
、$geoNear
、$changeStream
和$changeStreamSplitLargeEvent
阶段外,所有阶段都可以在管道中多次出现。
Note
For details on a specific command, including syntax and examples, click on the link to the command's reference page.有关特定命令的详细信息,包括语法和示例,请单击指向该命令参考页面的链接。
db.collection.aggregate( [ { <stage> }, ... ] )
$addFields | $project , $addFields reshapes each document in the stream; specifically, by adding new fields to output documents that contain both the existing fields from the input documents and the newly added fields.$project 类似,$addFields 会重塑流中的每个文档;具体来说,通过向输出文档添加新字段,这些字段既包含输入文档中的现有字段,也包含新添加的字段。$set $addFields .$addFields 的别名。 |
$bucket | |
$bucketAuto | |
$changeStream | |
$changeStreamSplitLargeEvent | $changeStreamSplitLargeEvent in a $changeStream pipeline and it must be the final stage in the pipeline.$changeStream 管道中使用$changeStreamSplitRateEvent ,并且它必须是管道中的最后一个阶段。 |
$collStats | |
$count | $count aggregation accumulator.$count 聚合累加器。 |
$densify | |
$documents | |
$facet | |
$fill | null and missing field values within documents.null 字段值和缺失的字段值。 |
$geoNear | $match , $sort , and $limit for geospatial data. $match 、$sort 和$limit 功能。 |
$graphLookup | |
$group | |
$indexStats | |
$limit | |
$listClusterCatalog | Retrieves information for collections in a cluster, including names and creation options. |
$listSampledQueries | Lists sampled queries for all collections or a specific collection. |
$listSearchIndexes | |
$listSessions | system.sessions collection.system.sessions 集合的会话。 |
$lookup | |
$match | $match |
$merge | $merge stage, it must be the last stage in the pipeline.$merge 阶段,它必须是管道中的最后一个阶段。 |
$out | $out stage, it must be the last stage in the pipeline.$out 阶段,它必须是管道中的最后一个阶段。 |
$planCacheStats | Returns plan cache information for a collection. |
$project | Reshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document.
See also |
$querySettings | Returns query settings previously added with setQuerySettings .New in version 8.0. |
$queryStats | WARNING: |
$rankFusion | $rankFusion de-duplicates input pipeline results and uses the Reciprocal Rank Fusion algorithm to create a final document ranking. |
$redact | Reshapes each document in the stream by restricting the content for each document based on information stored in the documents themselves. Incorporates the functionality of $project and $match . |
$replaceRoot | _id field. _id 字段。$replaceWith $replaceRoot stage.$replaceRoot 阶段的别名。 |
$replaceWith | Replaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the _id field. Specify a document embedded in the input document to promote the embedded document to the top level.$replaceWith is an alias for $replaceRoot stage. |
$sample | |
$search | |
$searchMeta | Returns different types of metadata result documents for the MongoDB Search query against an Atlas collection. To learn more, see MongoDB Search Aggregation Pipeline Stages. |
$set | Adds new fields to documents. Similar to $project , $set reshapes each document in the stream; specifically, by adding new fields to output documents that contain both the existing fields from the input documents and the newly added fields.
|
$setWindowFields | Groups documents into windows and applies one or more operators to the documents in each window. New in version 5.0. |
$skip | Skips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline. For each input document, outputs either zero documents (for the first n documents) or one document (if after the first n documents). |
$sort | Reorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified. For each input document, outputs one document. |
$sortByCount | Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group. |
$unionWith | Performs a union of two collections; i.e. combines pipeline results from two collections or views into a single result set. |
$unset | Removes/excludes fields from documents. |
$unwind | Deconstructs an array field from the input documents to output a document for each element. Each output document replaces the array with an element value. For each input document, outputs n documents where n is the number of array elements and can be zero for an empty array. |
$vectorSearch | Performs an ANN or ENN search on a vector in the specified field of an Atlas collection.
New in version 7.0.2. |
To learn about expressions that you can use in pipeline stages, see Expressions.要了解可以在管道阶段中使用的表达式,请参阅表达式。
db.aggregate()
Stages阶段
MongoDB also provides the MongoDB还提供了db.aggregate()
method:db.aggregate()
方法:
db.aggregate( [ { <stage> }, ... ] )
The following stages use the 以下阶段使用db.aggregate()
method and not the db.collection.aggregate()
method.db.aggregate()
方法,而不是db.collection.aggregate()
方法。
$changeStream | Returns a Change Stream cursor for the collection. This stage can only occur once in an aggregation pipeline and it must occur as the first stage. |
$currentOp | |
$documents | |
$listLocalSessions | Lists all active sessions recently in use on the currently connected mongos or mongod instance. These sessions may have not yet propagated to the system.sessions collection. |
Stages Available for Updates可用于更新的阶段
You can use the aggregation pipeline for updates in:您可以在以下位置使用聚合管道进行更新:
mongosh | |
---|---|
findAndModify | |
update |
For the updates, the pipeline can consist of the following stages:对于更新,管道可以由以下阶段组成:
$addFields
and its alias及其别名$set
$project
and its alias及其别名$unset
$replaceRoot
and its alias及其别名$replaceWith