MongoDB教程
MongoDB运算符
MongoDB命令
MongoDB数据库
MongoDB Shell
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>
}
命令字段:
字段 类型 说明
aggregate string 它包含聚合管道的名称
pipeline array 将文档列表转换为聚合管道一部分的数组。
explain boolean explain字段是可选的,用于返回有关管道处理的信息。
allowDiskUse boolean 它使命令能够写入临时文件。
cursor document 它处理包含用于创建光标对象的控制选项的文档。
maxTimeMS non-negative integer 它定义了对游标进行处理的时间限制。
Bypass
Document
Validation
boolean 仅在您指定out或merge聚合阶段时才适用。
readConcern document 它指定读取关注。可能的阅读关注级别为-本地,可用,多数和可线性化。
collation document 我们可以使用排序规则为字符串比较指定特定于语言的规则。例如-大小写和重音符号规则。
collation: 
{ locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>}
hint string or document 它通过索引名称或索引规范文档声明索引。
comment string 我们可以声明一个任意字符串,以帮助通过数据库探查器,currentOp和日志来跟踪操作。
writeConcern document 设置为在$out或$merge管道阶段使用默认写关注。
示例:
本文中包含以下文档:
{
   _id: ObjectId("52769ea0f3dc6ead47c9a1b2"),
   author: "Ankit",
   title: "lidihuo",
   tags: [ "Java Tutorial", "DBMS Tutorial", "mongodb"]
}
现在,我们将对articles集合执行汇总操作,以计算出现在集合中的tag数组中每个不同元素的计数。
db.runCommand( { aggregate: "articles",
   pipeline: [
      { $project: { tags: 1 } },
      { $unwind: "$tags" },
      { $group: { _id: "$tags", count: { $sum : 1 } } }
   ],
   cursor: { }
} )

MongoDB计数命令

MongoDB计数命令对集合或视图中的文档数进行计数。它返回包含计数和命令状态的文档。
语法:
{
  count: <collection or view>,
  query: <document>,
  limit: <integer>,
  skip: <integer>,
  hint: <hint>,
  readConcern: <document>,
  collation: <document>
}
命令字段
字段 类型 说明
count string 这是要计数的集合或视图的名称。
query document 它是可选的,用于选择要在集合或视图中计数的文档。
limit integer 它是可选的,用于限制要返回的匹配文档的最大数量。
skip integer 它是可选的,用于匹配文档以在返回结果之前跳过。
hint string 它用于将索引名称定义为字符串或索引规范文档。
readConcern document 它指定读取关注。
collation document 它允许我们定义特定于语言的规则以进行字符串比较。
语法:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
示例:
要计算集合中所有文档的数量:
db.runCommand( { count: 'orders' } )

MongoDB Distinct命令

此命令在单个集合中查找给定字段的不同值。它返回包含不同值数组的文档。返回文档包含带有查询统计信息和查询计划的嵌入式记录。
语法:
  distinct: "<collection>",
  key: "<field>",
  query: <query>,
  readConcern: <read concern document>,
  collation: <collation document>
  }
命令字段
字段 类型 说明
distinct string 它是查询不同值的集合的名称
key string 这是命令为其返回唯一值的字段。
query document 它指定将从中检索不同值的文档。
readConcern document 它指定读取关注。
 readConcern: {级别: 
     
      } 
     
collation document 它允许我们定义特定于语言的规则以进行字符串比较。
语法:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
示例:
下面的示例从库集合中的所有文档返回不同的字段簿值:
db.runCommand ( { distinct: "library", key: "books" } )

MongoDB MapReduce命令

MapReduce命令允许我们在集合上运行map-reduce聚合操作。
语法:
 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>
               }
             )
命令字段
字段 类型 说明
MapReduce 收藏 这是我们要在其上执行map-reduce操作的集合的名称。
map 功能 这是一个JavaScript函数,用于关联或映射键值对。
reduce 功能 这是一个JavaScript函数,可将与特定键相关联的所有值简化为一个对象。
out string 它指定存储输出的位置。
query document 它指定用于确定输入到地图功能的文档的选择标准。
sort document 它对输入文档进行排序。
limit 号码 它指定要输入到map函数的最大文档数。
finalize 功能 它遵循reduce方法来修改输出。
scope document 这是一个选项,用于声明可在地图上访问的全局变量。
jsMode 布尔值 jsMode指定是否将中间数据转换为BSON格式。
verbose 布尔值 它指定是否在结果中包括计时信息。
bypass
Document
Validation
布尔值 它使map-reduce在操作过程中绕过文档验证。
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>
               }
             )

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4