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()
。Importantmongosh Method
This page documents a
mongosh
method. This is not the documentation for database commands or language-specific drivers, such as Node.js.For the database command, see the
aggregate
command.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:Thedb.aggregate()
method has the following syntax:db.aggregate()
方法具有以下语法:db.aggregate( [ <pipeline> ], { <options> } )
Thepipeline
parameter is an array of stages to execute. It must start with a compatible stage that does not require an underlying collection, such as$currentOp
or$listLocalSessions
.pipeline
参数是要执行的阶段数组。它必须从一个不需要底层集合的兼容阶段开始,例如$currentOp
或$listLocalSessions
。Theoptions
document can contain the following fields and values:options
文档可以包含以下字段和值:Field字段Type类型Description描述explain
boolean Optional.可选的。Specifies that the method should return 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 thedbPath
directory.true
时,聚合操作可以将数据写入dbPath
目录中的_tmp
子目录。See Interaction with有关示例,请参阅与allowDiskUseByDefault
for an example.allowDiskUseByDefault
的交互。
Starting in MongoDB 4.2, the profiler log messages and diagnostic log messages includes a从MongoDB 4.2开始,如果任何聚合阶段由于内存限制将数据写入临时文件,探查器日志消息和诊断日志消息都会包含一个usedDisk
indicator if any aggregation stage wrote data to temporary files due to memory restrictions.usedDisk
指示符。cursor
document Optional.可选的。Specifies the initial batch size for the cursor.指定游标的初始批处理大小。The value of thecursor
field is a document with the fieldbatchSize
.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 asMongoDB使用与db.killOp()
.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.这样可以插入不符合验证要求的文档。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:可能的读取问题级别为:"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."local"
相同。The query returns the instance's most recent data.查询返回实例的最新数据。"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从MongoDB 4.2开始,$out
stage cannot be used in conjunction with read concern"linearizable"
.$out
阶段不能与读取关注"linearizable"
一起使用。That is, if you specify也就是说,如果为"linearizable"
read concern fordb.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 fordb.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.不能为一个操作指定多个排序规则。例如,不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能为查找使用一个排序规则,为排序使用另一个排序顺序。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.通过索引名称或索引规范文档指定索引。Notecomment
string Optional.可选的。Users can specify an arbitrary string to help trace the operation through the database profiler, currentOp, and logs.用户可以指定任意字符串来帮助通过数据库探查器、currentOp和日志跟踪操作。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
的管道
$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" }
}
] )