db.aggregate()

On this page本页内容

Definition定义

New in version 3.6.版本3.6中的新功能。

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描述
    explain boolean

    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.多文档事务中不可用。

    allowDiskUse boolean

    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指示符。

    cursor document Optional.可选。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.有关语法和示例,请参阅指定初始批量大小
    maxTimeMS non-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只在其指定的中断点之一终止操作。

    bypassDocumentValidation boolean

    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.这样可以插入不符合验证要求的文档。

    New in version 3.2.版本3.2中的新功能。

    readConcern document

    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:可能的阅读关注级别包括:

    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阶段。

    collation document

    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选项语法如下所示:

    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.例如,不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能对查找使用一种排序规则,对排序使用另一种排序规则。

    New in version 3.4.版本3.4中的新功能。

    hint string 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.

    New in version 3.6.版本3.6中的新功能。

    comment string

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

    New in version 3.6.版本3.6中的新功能。

    writeConcern document

    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" }
   }
] )