聚合命令使用聚合管道进行聚合操作。聚合管线允许用户使用基于阶段的应用程序序列来从记录或其他源执行数据处理。
语法:
{ aggregate: "<collection>" || 1, pipeline: [ <stage>, <...>], explain: <boolean>, allowDiskUse: <boolean>, cursor: <doc>, maxTimeMS: <int>, bypassDocumentValidation: <boolean>, readConcern: <doc>, collation: <doc>, hint: <string or doc>, comment: <string>, writeConcern: <doc> }
命令字段:
字段 | 类型 | 描述 |
---|---|---|
aggregate | string | 它包含聚合或管道名称 |
pipeline | array | 将文档列表中的数组转换为聚合管道的一部分。 |
explain | boolean | 解释字段是可选的,用于返回有关管道处理的信息。 |
allowdiskuse | boolean | 它使命令能够写入临时文件。 |
cursor | document | 它地址包含创建游标对象的控件选项的文档。 |
maxtimems | 非负整数 | 它定义了光标上的处理操作的时间限制。 |
bypass document validation | boolean | 适用于SE Profiler,Currentop和Logs。 |
writeConcern | document | 它设置为使用$OUT或$MERGE管道阶段使用默认写入 |
示例: 无涯教程在文档中有以下文件:
{ _id: ObjectId("52769ea0f3dc6ead47c9a1b2"), author: "Ankit", title: "Learnfk", tags: [ "Java Tutorial", "DBMS Tutorial", "mongodb"] }
现在,无涯教程将在文档集合上执行聚合操作,以计算在集合中出现的标签数组中的每个不同元素的计数。
db.runCommand( { aggregate: "articles", pipeline: [ { $project: { tags: 1 } }, { $unwind: "$tags" }, { $group: { _id: "$tags", count: { $sum : 1 } } } ], cursor: { } } )
MongoDB Count命令用来计算集合或视图中的文档数。它返回包含计数和命令状态的文档。
语法:
{ count: <collection or view>, query: <document>, limit: <integer>, skip: <integer>, hint: <hint>, readConcern: <document>, collation: <document> }
命令字段
Field | Type | Description |
---|---|---|
count | string | 它是集合或视图的名称。 |
query | document | 它是可选的,并用于在集合或视图中选择要计入的文档。 |
limit | integer | 它是可选的,用于限制要返回的最大匹配文件数。 |
skip | integer | 它是可选的,用于匹配文档以在返回结果之前跳过。 |
hint | string | 它用于将索引名称定义为字符串或索引规范文档。 |
readConcern | document | 它指定了读取的数据。readConcern: { level: <value> } |
collation | document | 它允许无涯教程为字符串比较定义特定于语言的规则。 语法:collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> } |
例子:
要计算集合中所有文档的数量:
db.runCommand( { count: 'orders' } )
此命令在单个集合中找到给定字段的不同值。它返回包含不同值数组的文档。返回文档包含具有查询统计信息和查询计划的嵌入式记录。
语法:
distinct: "<collection>", key: "<field>", query: <query>, readConcern: <read concern document>, collation: <collation document> }
示例:
以下示例返回来自库集合中所有文档的字段书籍的不同值:
db.runCommand ( { distinct: "library", key: "books" } )
MapReduce命令允许无涯教程通过集合运行映射 - 减少聚合操作。
语法:
db.runCommand( { mapReduce: <collection>, map: <function>, reduce: <function>, finalize: <function>, out: <output>, query: <document>, sort: <document>, limit: <number>, scope: <document>, jsMode: <boolean>, verbose: <boolean>, bypassDocumentValidation: <boolean>, collation: <document>, writeConcern: <document> } )
命令字段
Field | Type | Description |
---|---|---|
MapReduce | collection | 它是无涯教程想要执行 map-reduce操作的集合的名称。 |
map | function | 它是一个JavaScript函数,其关联或映射键值对。 |
reduce | function | 它是一个JavaScript函数,它缩短到单个对象所有与特定键相关的值。 |
out | string | 它指定存储输出的位置。 |
query | document | 它指定选择标准,以确定输入到map函数的文档。 |
sort | document | 它会对输入文档进行排序。 |
limit | number | 它指定输入到map函数的最大文档数。 |
finalize | function | 它遵循缩短方法来修改输出。 |
scope | document | 它是一个选项,并声明地图上可访问的全局变量。 |
jsMode | boolean | JSMode指定是否将中间数据转换为BSON格式。 |
verbose | boolean | 它指定是否包括结果中的定时信息。 |
bypass Document Validation | boolean | 它可以在操作期间映射减少到绕过文档验证。 |
collation | document | 它允许无涯教程为字符串比较指定特定于语言的规则。collation: { locale: <string>, caseLevel: <boolean>, caseFirst: <string>, strength: <int>, numericOrdering: <boolean>, alternate: <string>, maxVariable: <string>, backwards: <boolean> } |
writeConcern | document | 它是一种表达在输出到集合时使用的写入问题的文档。 |
示例:
var mapFunction = function() { ... }; var reduceFunction = function(key, values) { ... }; db.runCommand( { mapReduce: <input-collection>, map: mapFunction, reduce: reduceFunction, out: { merge: <output-collection> }, query: <query> } )
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕