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()。Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the有关数据库命令,请参阅aggregatecommand.aggregate命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。Thedb.aggregate()method has the following syntax:db.aggregate()方法具有以下语法:db.aggregate( [ <pipeline> ], { <options> } )Thepipelineparameter is an array of stages to execute.pipeline参数是要执行的阶段数组。It must start with a compatible stage that does not require an underlying collection, such as它必须从不需要底层集合的兼容阶段开始,例如$currentOpor$listLocalSessions.$currentOp或$listLocalSessions。Theoptionsdocument can contain the following fields and values:options文档可以包含以下字段和值:Field字段Type类型Description描述explainboolean布尔值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.在多文档事务中不可用。allowDiskUseboolean布尔值Optional.可选。Enables writing to temporary files. When set to允许写入临时文件。当设置为true, aggregation operations can write data to the_tmpsubdirectory in thedbPathdirectory.true时,聚合操作可以将数据写入dbPath目录中的_tmp子目录。See Interaction with有关示例,请参阅与allowDiskUseByDefaultfor an example.allowDiskUseByDefault的交互。The profiler log messages and diagnostic log messages includes a如果由于内存限制,任何聚合阶段将数据写入临时文件,分析器日志消息和诊断日志消息都会包含一个usedDiskindicator if any aggregation stage wrote data to temporary files due to memory restrictions.usedDisk指示符。cursordocument文档Optional.可选。Specifies the initial batch size for the cursor. The value of the指定游标的初始批大小。cursorfield is a document with the fieldbatchSize.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值0explicitly 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仅在其指定的中断点之一终止操作。bypassDocumentValidationboolean布尔值Optional.可选。Applicable only if you specify the仅当指定$outor$mergeaggregation stages.$out或$merge聚合阶段时才适用。Enables启用db.collection.aggregate()to bypass schema validation during the operation. This lets you insert documents that do not meet the validation requirements.db.collection.aggregate()以在操作期间绕过架构验证。这允许您插入不符合验证要求的文档。readConcerndocument文档Optional.可选。Specifies the read concern.指定读取关注。ThereadConcernoption has the following syntax: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"在primary和非分片次级上的行为相同。查询返回实例的最新数据。"majority". Available for replica sets that use WiredTiger storage engine.。适用于使用WiredTiger存储引擎的副本集。"linearizable". Available for read operations on the。仅适用于primaryonly.primary上的读取操作。"snapshot". Available for multi-document transactions and certain read operations outside of multi-document transactions.。可用于多文档事务和多文档事务之外的某些读取操作。
For more formation on the read concern levels, see Read Concern Levels.有关读取关注级别的更多信息,请参阅读取关注级别。The$outstage cannot be used in conjunction with read concern"linearizable".$out阶段不能与读取关注"linearizable"结合使用。If you specify如果为"linearizable"read concern fordb.collection.aggregate(), you cannot include the$outstage in the pipeline.db.collection.aggregate()指定"linearizable"读取关注,则不能在管道中包含$out阶段。The$mergestage 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$mergestage 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指定排序规则时,localefield is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.locale字段是必填的;所有其他排序字段都是可选的。有关字段的描述,请参阅排序规则文档。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.通过索引名称或索引规范文档指定索引。The该提示不适用于hintdoes not apply to$lookupand$graphLookupstages.$lookup和$graphLookup阶段。commentstring字符串Optional.可选。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表达与$outor$mergestage.$out或$merge阶段一起使用的写入关注的文档。Omit to use the default write concern with the省略在$outor$mergestage.$out或$merge阶段使用默认写入关注。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- 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的源代码可用、免费使用和自我管理版本
Example示例
Pipeline with $currentOp使用$currentOp的管道
$currentOpThe 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" }
}
] )