db.aggregate()

On this page本页内容

Definition定义

db.aggregate()

Runs a specified admin/diagnostic pipeline which does not require an underlying collection. 运行不需要基础集合的指定管理/诊断管道。For aggregations on collection data, see db.collection.aggregate().有关集合数据的聚合,请参阅db.collection.aggregate()

The db.aggregate() method has the following syntax:db.aggregate()方法具有以下语法:

db.aggregate( [ <pipeline> ], { <options> } )
  • The pipeline parameter is an array of stages to execute. pipeline参数是要执行的阶段数组。It must start with a compatible stage that does not require an underlying collection, such as $currentOp or $listLocalSessions.它必须从不需要底层集合的兼容阶段开始,例如$currentOp$listLocalSessions
  • The options document can contain the following fields and values:options文档可以包含以下字段和值:

    Field字段Type类型Description描述
    explainboolean

    Optional. 可选。Specifies to return the information on the processing of the pipeline. 指定返回有关管道处理的信息。See Return Information on Aggregation Pipeline Operation for an example.有关示例,请参阅聚合管道操作的返回信息

    Not available in multi-document transactions.多文档事务处理中不可用。

    allowDiskUseboolean

    Optional. 可选。Enables writing to temporary files. 允许写入临时文件。When set to true, aggregation operations can write data to the _tmp subdirectory in the dbPath directory. 当设置为true时,聚合操作可以将数据写入dbPath目录中的_tmp子目录。See Perform Large Sort Operation with External Sort for an example.有关示例,请参阅使用外部分拣执行大型分拣操作

    Starting in MongoDB 4.2, the profiler log messages and diagnostic log messages includes a usedDisk indicator if any aggregation stage wrote data to temporary files due to memory restrictions.从MongoDB 4.2开始,如果任何聚合阶段由于内存限制将数据写入临时文件,探查器日志消息诊断日志消息都包含usedDisk指示符。

    cursordocumentOptional. 可选。Specifies the initial batch size for the cursor. 指定游标的初始批量大小。The value of the cursor field is a document with the field batchSize. cursor字段的值是具有字段batchSize的文档。See Specify an Initial Batch Size for syntax and example. 有关语法和示例,请参阅指定初始批量大小
    maxTimeMSnon-negative integer非负整数

    Optional. 可选。Specifies a time limit in milliseconds for processing operations on a cursor. 指定处理游标操作的时间限制(毫秒)。If you do not specify a value for maxTimeMS, operations will not time out. 如果未指定maxTimeMS的值,操作将不会超时。A value of 0 explicitly specifies the default unbounded behavior.0明确指定默认的无边界行为。

    MongoDB terminates operations that exceed their allotted time limit using the same mechanism as db.killOp(). MongoDB使用与db.killOp()相同的机制终止超过分配时间限制的操作。MongoDB only terminates an operation at one of its designated interrupt points.MongoDB仅在其指定的中断点之一处终止操作。

    bypassDocumentValidationboolean

    Optional. 可选。Applicable only if you specify the $out or $merge aggregation stages.仅当指定$out$merge聚合阶段时才适用。

    Enables db.collection.aggregate() to bypass document validation during the operation. 启用db.collection.aggregate()以在操作期间绕过文档验证。This lets you insert documents that do not meet the validation requirements.这允许您插入不符合验证要求的文档。

    readConcerndocument

    Optional. 可选。Specifies the read concern.指定读取问题

    Starting in MongoDB 3.6, the readConcern option has the following syntax: 从MongoDB 3.6开始,readConcern选项具有以下语法:readConcern: { level: <value> }

    Possible read concern levels are:可能的读取问题级别包括:

    • "local". This is the default read concern level for read operations against the primary and secondaries.。这是针对主设备和辅助设备的读取操作的默认读取关注级别。
    • "available". Available for read operations against the primary and secondaries. 。可用于针对主要和次要磁盘的读取操作。"available" behaves the same as "local" against the primary and non-sharded secondaries. The query returns the instance's most recent data.对于主分区和非分片的次分区,"available"的行为与"local"相同。查询返回实例的最新数据。
    • "majority". Available for replica sets that use WiredTiger storage engine.。适用于使用WiredTiger存储引擎的副本集。
    • "linearizable". Available for read operations on the primary only.。仅适用于primary上的读取操作。

    For more formation on the read concern levels, see Read Concern Levels.有关读取关注级别的更多信息,请参阅读取关注级别

    Starting in MongoDB 4.2, the $out stage cannot be used in conjunction with read concern "linearizable". 从MongoDB 4.2开始,$out阶段不能与读取关注点"linearizable"结合使用。That is, if you specify "linearizable" read concern for db.collection.aggregate(), you cannot include the $out stage in the pipeline.也就是说,如果为db.collection.aggregate()指定"linearizable"读取关注点,则不能在管道中包含$out阶段。

    The $merge stage cannot be used in conjunction with read concern "linearizable". $merge阶段不能与读取关注点"linearizable"结合使用。That is, if you specify "linearizable" read concern for db.collection.aggregate(), you cannot include the $merge stage in the pipeline.也就是说,如果为db.collection.aggregate()指定"linearizable"读取关注点,则不能在管道中包含$merge阶段。

    collationdocument

    Optional.可选择的

    Specifies the collation to use for the operation.指定用于操作的排序规则

    Collation排序规则 allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.允许用户为字符串比较指定特定于语言的规则,例如字母大小写和重音标记的规则。

    The collation option has the following syntax:排序选项具有以下语法:

    collation: {
       locale: <string>,
       caseLevel: <boolean>,
       caseFirst: <string>,
       strength: <int>,
       numericOrdering: <boolean>,
       alternate: <string>,
       maxVariable: <string>,
       backwards: <boolean>
    }

    When specifying collation, the locale field is mandatory; all other collation fields are optional. 指定排序规则时,locale字段是必需的;所有其他排序字段都是可选的。For descriptions of the fields, see Collation Document.有关字段的描述,请参阅排序规则文档。

    If the collation is unspecified but the collection has a default collation (see db.createCollection()), the operation uses the collation specified for the collection.如果未指定排序规则,但集合具有默认排序规则(请参阅db.createCollection()),则操作将使用为集合指定的排序规则。

    If no collation is specified for the collection or for the operations, MongoDB uses the simple binary comparison used in prior versions for string comparisons.如果没有为集合或操作指定排序规则,MongoDB将使用以前版本中使用的简单二进制比较进行字符串比较。

    You cannot specify multiple collations for an operation. 不能为一个操作指定多个排序规则。For example, you cannot specify different collations per field, or if performing a find with a sort, you cannot use one collation for the find and another for the sort.例如,不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能将一个排序规则用于查找,而将另一个用于排序。

    hintstring or document

    Optional. 可选。The index to use for the aggregation. 用于聚合的索引。The index is on the initial collection/view against which the aggregation is run.索引位于运行聚合的初始集合/视图上。

    Specify the index either by the index name or by the index specification document.通过索引名称或索引规范文档指定索引。

    Note注意

    The hint does not apply to $lookup and $graphLookup stages.hint不适用于$lookup$graphLookup阶段。

    commentstringOptional. 可选。Users can specify an arbitrary string to help trace the operation through the database profiler, currentOp, and logs. 用户可以指定任意字符串,以帮助通过数据库探查器、currentOp和日志跟踪操作。
    writeConcerndocument

    Optional. 可选。A document that expresses the write concern to use with the $out or $merge stage.表示要与$out$merge阶段一起使用的写入关注点的文档。

    Omit to use the default write concern with the $out or $merge stage.省略以使用$out$merge阶段的默认写关注点。

Example示例

Pipeline with $currentOp使用$currentOp的管道

The following example runs a pipeline with two stages. 以下示例运行具有两个阶段的管道。The first stage runs the $currentOp operation and the second stage filters the results of that operation.第一阶段运行$currentOp操作,第二阶段筛选该操作的结果。

use admin
db.aggregate( [ {
   $currentOp : { allUsers: true, idleConnections: true } }, {
   $match : { shard: "shard01" }
   }
] )
←  db.adminCommand()db.cloneDatabase() →