MongoDB - 聚合命令

MongoDB - 聚合命令 首页 / MongoDB入门教程 / MongoDB - 聚合命令

MongoDB汇总命令

聚合命令使用聚合管道进行聚合操作。聚合管线允许用户使用基于阶段的应用程序序列来从记录或其他源执行数据处理。

语法:

{
  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>
}

命令字段:

字段类型描述
aggregatestring它包含聚合或管道名称
pipelinearray将文档列表中的数组转换为聚合管道的一部分。
explain boolean解释字段是可选的,用于返回有关管道处理的信息。
allowdiskuse boolean它使命令能够写入临时文件。
cursordocument它地址包含创建游标对象的控件选项的文档。
maxtimems非负整数它定义了光标上的处理操作的时间限制。
bypass document validation boolean适用于SE Profiler,Currentop和Logs。
writeConcerndocument它设置为使用$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命令

MongoDB Count命令用来计算集合或视图中的文档数。它返回包含计数和命令状态的文档。

语法:

{
  count: <collection or view>,
  query: <document>,
  limit: <integer>,
  skip: <integer>,
  hint: <hint>,
  readConcern: <document>,
  collation: <document>
}

命令字段

FieldTypeDescription
countstring它是集合或视图的名称。
querydocument它是可选的,并用于在集合或视图中选择要计入的文档。
limitinteger它是可选的,用于限制要返回的最大匹配文件数。
skipinteger它是可选的,用于匹配文档以在返回结果之前跳过。
hintstring它用于将索引名称定义为字符串或索引规范文档。
readConcerndocument它指定了读取的数据。
readConcern: { level: <value> }
collationdocument它允许无涯教程为字符串比较定义特定于语言的规则。 语法:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}

例子:

要计算集合中所有文档的数量:

db.runCommand( { count: 'orders' } )

MongoDB独特命令

此命令在单个集合中找到给定字段的不同值。它返回包含不同值数组的文档。返回文档包含具有查询统计信息和查询计划的嵌入式记录。

语法:

  distinct: "<collection>",
  key: "<field>",
  query: <query>,
  readConcern: <read concern document>,
  collation: <collation document>
  }

示例:

以下示例返回来自库集合中所有文档的字段书籍的不同值:

db.runCommand ( { distinct: "library", key: "books" } )

mapreduce命令

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

命令字段

FieldTypeDescription
MapReducecollection它是无涯教程想要执行 map-reduce操作的集合的名称。
mapfunction它是一个JavaScript函数,其关联或映射键值对。
reducefunction它是一个JavaScript函数,它缩短到单个对象所有与特定键相关的值。
outstring它指定存储输出的位置。
querydocument它指定选择标准,以确定输入到map函数的文档。
sortdocument它会对输入文档进行排序。
limitnumber它指定输入到map函数的最大文档数。
finalizefunction它遵循缩短方法来修改输出。
scopedocument它是一个选项,并声明地图上可访问的全局变量。
jsModebooleanJSMode指定是否将中间数据转换为BSON格式。
verboseboolean它指定是否包括结果中的定时信息。
bypass
Document
Validation
boolean它可以在操作期间映射减少到绕过文档验证。
collationdocument它允许无涯教程为字符串比较指定特定于语言的规则。
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
writeConcerndocument它是一种表达在输出到集合时使用的写入问题的文档。

示例:

var mapFunction = function() { ... };
var reduceFunction = function(key, values) { ... };

db.runCommand(
               {
                 mapReduce: <input-collection>,
                 map: mapFunction,
                 reduce: reduceFunction,
                 out: { merge: <output-collection> },
                 query: <query>
               }
             )

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

如何做好一场技术演讲 -〔极客时间〕

编辑训练营 -〔总编室〕

检索技术核心20讲 -〔陈东〕

Django快速开发实战 -〔吕召刚〕

容器实战高手课 -〔李程远〕

深入C语言和程序运行原理 -〔于航〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

好记忆不如烂笔头。留下您的足迹吧 :)