当前位置:   article > 正文

5.MongoDB之正则表达式与聚合框架_mongo的正则

mongo的正则

关于正则表达式这里又称为模糊匹配;

关于聚合框架实际上应该包括聚合管道(aggregation pipeline)和MapReduce。

 一、模糊查询与正则表达式

 1、测试数据

  1. #首先插入如下几条数据
  2. db.mycollection.save({ "_id" : ObjectId("60176db81d49ac9562d09552"), "index_name" : "robot_date_webim_2852199203_821678010_801211737966432", "uniqueid" : "", "systime" : NumberLong("1612137600000000") })
  3. db.mycollection.save({ "_id" : ObjectId("60176dbb1d49ac9562d09553"), "systime" : NumberLong("1612148154742301"), "time" : { "value" : NumberLong("1612148154742301"), "type" : 3 }, "index_name" : "robot_webim_2852199203_821678010_801211737966432", "session_id" : { "value" : "webim_2852199203_801211737966432_1612148147671", "type" : 2 }, "uniqueid" : "1612148154_8115996", "content" : { "value" : "问题11\n姚明有多高\n姚明有多瘦\n姚明有多矮\n周杰伦会跳舞\n周杰伦会唱歌", "type" : 2 }})
  4. db.mycollection.save({ "_id" : ObjectId("60176dbe1d49ac9562d09554"), "content" : { "value" : "姚明有多高", "type" : 2 }, "uniqueid" : "1612148157_9306913", "session_id" : { "value" : "webim_2852199203_801211737966432_1612148147671", "type" : 2 }, "index_name" : "robot_webim_2852199203_821678010_801211737966432", "systime" : NumberLong("1612148157968382"), "time" : { "value" : NumberLong("1612148157968382"), "type" : 3 } })
  5. db.mycollection.save({ "_id" : ObjectId("60176dbf1d49ac9562d09555"), "session_id" : { "value" : "webim_2852199203_801211737966432_1612148147671", "type" : 2 }, "time" : { "value" : NumberLong("1612148159149489"), "type" : 3 }, "index_name" : "robot_webim_2852199203_821678010_801211737966432", "uniqueid" : "1612148159_9679002", "systime" : NumberLong("1612148159149489"), "content" : { "value" : "高", "type" : 2 } })
  6. db.mycollection.save({ "_id" : ObjectId("60176dc11d49ac9562d09556"), "index_name" : "robot_webim_2852199203_821678010_801211737966432", "uniqueid" : "1612148161_1992952", "systime" : NumberLong("1612148161059899"), "time" : { "type" : 3, "value" : NumberLong("1612148161059899") }, "session_id" : { "value" : "webim_2852199203_801211737966432_1612148147671", "type" : 2 }, "content" : { "value" : "刘翔想打篮球", "type" : 2 }})

2、find常规查询 

(1)查询某字段包含关键子"string"的记录,等同于sql的    like ‘%string%’

  1. #查询index_name包含关键字"date_webim"的记录(注:其中的关键词不能用双引号括起来)
  2. db.mycollection.find({"index_name":/date_webim/})
  3. db.mycollection.find({index_name:/date_webim/}) //这样也是可以的
  4. #查询content.value字段包括关键词"姚明"的记录
  5. db.mycollection.find({"content.value":/姚明/})
  6. #再附加其他条件当然也是ok的
  7. db.mycollection.find({systime:{"$gte":1612137600000000},"index_name":/date_webim/})

(2)查询某字段开头是"string"的记录,等同于sql的  like ‘string%’

  1. #查询index_name开头是"robot"的记录
  2. db.mycollection.find({"index_name":/^robot/})
  3. db.mycollection.find({index_name:/^robot/})
  4. #查询content.value开头是"姚明"的记录
  5. db.mycollection.find({"content.value":/^姚明/})

(4)查询某字段结尾是"string"的记录,等同于sql的 like ‘%string’

  1. #查询index_name结尾是"966432"的记录
  2. db.mycollection.find({"index_name":/966432$/})
  3. db.mycollection.find({index_name:/966432$/})
  4. #查询content.value结尾是"篮球"的记录
  5. db.mycollection.find({"content.value":/篮球$/})

3、$regex查询

使用$regex同样可以实现上述效果。

(1)$regex语法如下:

  1. { <field>: { $regex: /pattern/, $options: '<options>' } }
  2. { <field>: { $regex: 'pattern', $options: '<options>' } }
  3. { <field>: { $regex: /pattern/<options> } }

(2)$option选项

① i :加上 "$options":"$i" 表示忽略大小写;

② m:会更改^和$元字符的默认行为,分别使用与行的开头结尾匹配,而不是与输入字符串的开头和结尾匹配。

③ x: 忽略非转义的空白字符。设置x选项后正则表达式中的非转义的空白字符将会被忽略,同时井号(#)被解释为注释的开头注,只能显示位于option选项中。

④ s:单行匹配模式。设置s选项后会改变模式中的点号(.)元字符的默认行为,他会匹配所有字符,包括换行符(/n),只能显示位于option选项中。

(3)$regex实现上述匹配

1)查询某字段包含关键子"string"的记录,等同于sql的    like ‘%string%’

  1. db.mycollection.find({index_name:{$regex:"date_webim"}})
  2. db.mycollection.find({"content.value":{$regex:"姚明"}})
  3. db.mycollection.find({index_name:{$regex:"WEBIM",$options:$i}})

2)查询某字段开头是"string"的记录,等同于sql的  like ‘string%’

db.mycollection.find({"content.value":{$regex:"^姚明"}})

3)查询某字段结尾是"string"的记录,等同于sql的 like ‘%string’

db.mycollection.find({"content.value":{$regex:"唱歌$"}})

二、mongodb聚合框架   参考

0、单一目的的聚合方法

        最典型的就是count、distinct。

(1)count:统计满足查询条件的消息条数

(2)distinct:获取满足查询条件的指定字段的不重复值,以数组形式返回。

用法如下:

  1. db.collection_name.distinct(field,query,options)
  2. field:指定返回的字段(string
  3. query:条件查询(document)
  4. option:其他的选项(document),查看options

使用举例:

  1. db.collection_event_track_96.distinct('kfuin')
  2. db.collection_event_track_96.distinct("kfuin")
  3. db.collection_event_track_96.distinct("kfuin")
  4. db.collection_event_track_96.distinct("kfuin",{systime:{$gt:16319337580000}})
  5. db.collection_event_track_96.distinct("kfuin",{"$and":[{systime:{$gt:1631093568000000}},{systime:{$lt:1631933758000000}}]})

1、聚合管道(aggeregate pipeline)

聚合管道:顾名思义就是基于管道模式的聚合框架,简单来说就是在聚合管道中前一个阶段处理的结果会作为后一阶段处理的输入,documents通过管道中的各个阶段处理后得到最终的聚合结果。

0、一个实例

 对于集合mkt_track_detail_878中的如下数据,可以通过如下pipeline语句聚合到

  1. {
  2. "_id":"b143faa06c500f8c8e6a55c3ccb012f2",
  3. "kfuin":2852150878,
  4. "mkt_type":14,
  5. "sa_list":[
  6. "3_wx50dedeabf88ac326_o8jkBwHYScxHy-wvBsBZ3Fs4J9BA"
  7. ],
  8. "sensor_properties":{
  9. "$marketing_type":"服务号运营",
  10. "$name_of_marketing_event":"企点营销测试号",
  11. "$sdk_type":"openapi",
  12. "$utm_source":"公众号微信回调",
  13. "ContentType":"文本",
  14. "SendContent":"12314",
  15. "$appid":"wx50dedeabf88ac326",
  16. "$ip":"9.148.128.234"
  17. },
  18. "sensor_event":"粉丝发送消息",
  19. "sensor_type":"track",
  20. "session_id":"o8jkBwHYScxHy-wvBsBZ3Fs4J9BA_1619607958907",
  21. "time":1619608209000000
  22. }
  1. db.mkt_track_detail_878.aggregate(
  2. [
  3. {
  4. $match:{
  5. kfuin:2852150878,
  6. sa_list:{"$in":["3_wx50dedeabf88ac326_o8jkBwHYScxHy-wvBsBZ3Fs4J9BA","1_1241133123"]},
  7. "sensor_event" : "粉丝发送消息",
  8. "sensor_properties.ContentType" : {
  9. "$in" : [ "文本", "文章" ]
  10. },
  11. "time" : {
  12. "$gte" : 1604061021000000
  13. }
  14. }
  15. },
  16. {
  17. "$group" : {
  18. "_id" : null,
  19. "count" : {
  20. "$sum" : 1
  21. }
  22. }
  23. }
  24. ]
  25. )

1、mongo聚合的stage与mysql对应

mongo聚合主要有如下stage。$count , $group,  $match, $project,  $unwind, $limit, $skip,  $sort, $sortByCount,  $lookup, $out, $addFields

SQL 操作/函数   

mongodb聚合操作

where

$match

用于过滤数据,只输出符合条件的文档;$match使用MongoDB的标准查询操作。

group by

$group

将集合中的文档分组,可用于统计结果。

having

$match

在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。

select

$project

修改输入文档的结构。可用来重命令、增加或删除域,也可用于创建计算结果即嵌套文档。

order by

$sort

输出文档排序后输出

limit 

$limit

用来限制MongoDB返回的文档树

sum()

$sum

求和累加

count()

$sum

行数汇总

join

$lookup  

(v3.2 新增)

$count返回包含输入到stage的文档的计数,理解为返回与表或试图的find()查询匹配的文档的计数。即不执行find()操作,而是计数并返回与查询匹配的结果数。

2、聚合过程

db.collection.aggregate()是基于数据处理的聚合管道,每个文档通过一个由多个阶段(stage)组成的管道,可以对每个阶段的管道进行分组、过滤等功能;然后通过一些列的处理,输出相应的结果。通过下图可以了解Aggregate处理的过程。

(1)db.collection.aggregate()可以用多个构建创建一个管道,对一连串的文档进行处理。这些构建包括:筛选操作match、映射操作project、分组操作group、排序操作sort、限制操作limit、跳过操作skip等。

(2)db.collection.aggregate()使用了MongoDB内置的原生操作,聚合效率非常高。通过这种方式SQL的group by的功能,即用户不编写JS脚本就可以实现聚合的复杂操作。

(3)每个阶段管道限制为100MB内存。如果一个节点管道超过这个极限,MongoDB会产生错误。为了处理大型数据集用户可以通过设置allowDiskUse为true方方式吧数据写入临时文件。这样就就解决了100M内存的限制。

(4)db.collection.aggregate()可以作用于分片集合,但结果不能输在分片集合;MapReduce可以作用于分片集合,结果也可以输在分片集合。

(5)db.collection.aggregate()方法可以返回一个指针(cursor),数据放在内存中,直接操作。跟Mongo shell 一样指针操作。
(6)db.collection.aggregate()输出的结果只能保存在一个文档中,BSON Document大小限制为16M。可以通过返回指针解决,版本2.6中后面:DB.collect.aggregate()方法返回一个指针,可以返回任何结果集的大小。

3、 聚合的基本语法格式如下:

db.collection.aggregate(pipelineoptions)   

其中:

pipelinearray在版本2.6中更改:该方法仍然可以接受管道阶段作为独立的参数,而不是数组中的元素;如果不将管道指定为数组,则不能指定options参数。
optionsdocument可选的 只有在将管道指定为数组时才可用。

示例如下:

 

options选项文档可以包含一下字段和值:

explainboolean可选。指定返回关于管道处理的信息(ps:这个explain好像远远比不了find语句的explain方法)
allowDiskUseboolean可选。允许写入临时文件。当设置为true时,聚合操作可以将数据写入dbPath目录中的_tmp子目录
cursordocument

可选。指定游标的初始批处理大小。该cursor 字段的值是具有该字段的文档batchSize

maxTimeMS非负整数

可选。指定处理游标操作的时间限制(以毫秒为单位)。如果未指定maxTimeMS的值,则操作不会超时。值0显式指定默认的无界行为。

MongoDB使用与之相同的机制终止超出其分配时间限制的操作db.killOp()。MongoDB仅终止其指定中断点之一的操作。

bypassDocumentValidationboolean可选。只有在指定$out聚合操作符时才可用。
使db.collection。聚合以在操作期间绕过文档验证。这样可以插入不满足验证要求的文档。
readConcerndocument

可选的。指定读取问题

readConcern选项具有以下语法:

readConcern: { level: <value> }
collationdocument

可选。

指定 要用于操作的排序规则

排序规则允许用户为字符串比较指定特定于语言的规则,例如字母和重音标记的规则。

排序规则选项具有以下语法:

 
  1. collation: {

  2. locale: <string>,

  3. caseLevel: <boolean>,

  4. caseFirst: <string>,

  5. strength: <int>,

  6. numericOrdering: <boolean>,

  7. alternate: <string>,

  8. maxVariable: <string>,

  9. backwards: <boolean>

  10. }

指定排序规则时,该locale字段是必填字段; 所有其他校对字段都是可选的。有关字段的说明,请参阅排序文档

如果未指定排序规则但集合具有默认排序规则(请参阅参考资料db.createCollection()),则该操作将使用为集合指定的排序规则。

如果没有为集合或操作指定排序规则,MongoDB使用先前版本中使用的简单二进制比较进行字符串比较。

您无法为操作指定多个排序规则。例如,您不能为每个字段指定不同的排序规则,或者如果使用排序执行查找,则不能对查找使用一个排序规则,而对排序使用另一个排序规则。

hintstring or document

可选。用于聚合的索引。索引位于运行聚合的初始集合/视图上。

通过索引名称或索引规范文档指定索引。

注意

hint不适$lookup和 $graphLookup阶段。

commentstring

可选。用户可以指定任意字符串,以帮助通过数据库事件探查器,currentOp和日志跟踪操作。

 可能报错:

1、聚合结果限制16M以内,否则报错。

Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in

原因是聚合的结果必须要限制在16M以内操作,(mongodb支持的最大影响信息的大小),否则必须放在磁盘中做缓存(allowDiskUse=True)。其实在[]后面加上",{allowDiskUse:true}"就好了。

应用举例一:

  1. #首先插入如下测试数据:
  2. db.mycollection.save({ "_id" : 1, "subject" : "History", "score" : 88 })
  3. db.mycollection.save({ "_id" : 2, "subject" : "History", "score" : 92 })
  4. db.mycollection.save({ "_id" : 3, "subject" : "History", "score" : 97 })
  5. db.mycollection.save({ "_id" : 4, "subject" : "History", "score" : 71 })
  6. db.mycollection.save({ "_id" : 5, "subject" : "History", "score" : 79 })
  7. db.mycollection.save({ "_id" : 6, "subject" : "History", "score" : 83 })

(1)$count:

  1. #语法
  2. {$count: <string>} #后面的string你给输出设置的属性

执行:
1)$match 阶段排除score小于等于80的文档,将大于80的文档传到下个阶段
2)$count阶段返回聚合管道中剩余文档的计数,并将该值分配给名为passing_scores的字段。
执行结果:

  1. mongos> db.mycollection.aggregate(
  2. [
  3. {$match:{score:{"$gt":80}}},
  4. {$count:"number_of_pass_80"}
  5. ]
  6. )
  7. { "number_of_pass_80" : 4 }

(2)$group

释义:按指定的表达式对文档进行分组,并将不同分组的文档输出到下一个阶段。输出的的文档包含一个_id字段,该字段按键包含不同的组。输出文档还可以包含计算字段,该字段保存由$group的_id字段分布的一些accumulator表达式的值。$group不会输出具体的文档而只是统计信息。

  1. #语法
  2. { $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }

1)_id字段是必填的;但是可以通过为其制定为null的方式来为整个输出文档计算累计值。

2)剩余的计算字段是可选的,并使用<accumulator>运算符进行计算。

3)_id和<accumulator>表达式可以接受任何有效的表达式。accumulator支持的操作符如下:

名称                      描述类比sql
$avg计算均值     avg
$first返回每组第一个文档,如果有排序,按照排序,如果没有按照默认的存储的顺序的第一个文档。limit 0,1
$last返回每组最后一个文档,如果有排序,按照排序,如果没有按照默认的存储的顺序的最后个文档。-
$max根据分组,获取集合中所有文档对应值得最大值。max
$min根据分组,获取集合中所有文档对应值得最小值。min
$push将指定的表达式的值添加到一个数组中。-
$addToSet 将表达式的值添加到一个集合中(无重复值,无序)。-
$sum计算总和sum
$stdDevPop返回输入值的总体标准偏差(population standard deviation)-
$stdDevSamp返回输入值的样本标准偏差(the sample standard deviation)-

$group阶段的内存限制为100M。默认情况下,如果stage超过此限制,$group将产生错误。但是,要允许处理大型数据集,请将allowDiskUse选项设置为true以启用$group操作以写入临时文件。

注:如果想让输出好看的话就在结尾加个.pretty()。

  1. #首先插入如下待测数据
  2. db.mycollection.save({ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") })
  3. db.mycollection.save({ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") })
  4. db.mycollection.save({ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") })
  5. db.mycollection.save({ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") })
  6. db.mycollection.save({ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") })
  7. #一、接下来一月份/日期/年份对文档进行分组,并计算每个分组的总价totalPrice、平均数量average quantity;并且计算每个组的文档数量
  8. mongos> db.mycollection.aggregate(
  9. [
  10. {
  11. $group:{
  12. _id: {
  13. month:{$month:"$date"},
  14. day:{$dayOfMonth:"$date"},
  15. year:{$year:"$date"}
  16. },
  17. totalPrice:{$sum:{$multiply:["$price","$quantity"]}},
  18. averageQuantity:{$avg:"$quantity"},
  19. count:{$sum:1}
  20. }
  21. }
  22. ]
  23. ).pretty()
  24. {
  25. "_id" : {
  26. "month" : null,
  27. "day" : null,
  28. "year" : null
  29. },
  30. "totalPrice" : 0,
  31. "averageQuantity" : null,
  32. "count" : 1
  33. }
  34. {
  35. "_id" : {
  36. "month" : 4,
  37. "day" : 4,
  38. "year" : 2014
  39. },
  40. "totalPrice" : 200,
  41. "averageQuantity" : 15,
  42. "count" : 2
  43. }
  44. {
  45. "_id" : {
  46. "month" : 3,
  47. "day" : 15,
  48. "year" : 2014
  49. },
  50. "totalPrice" : 50,
  51. "averageQuantity" : 10,
  52. "count" : 1
  53. }
  54. {
  55. "_id" : {
  56. "month" : 3,
  57. "day" : 1,
  58. "year" : 2014
  59. },
  60. "totalPrice" : 40,
  61. "averageQuantity" : 1.5,
  62. "count" : 2
  63. }
  64. #二、group null。以下聚合操作将指定组_id为null;其效果是计算集合中所有文档的总价格和平均数量及其技术。
  65. db.mycollection.aggregate(
  66. [
  67. {
  68. $group:{
  69. _id: null,
  70. totalPrice:{$sum:{$multiply:["$price","$quantity"]}},
  71. averageQuantity:{$avg:"$quantity"},
  72. count:{$sum:1}
  73. }
  74. }
  75. ]
  76. )
  77. {
  78. "_id" : null,
  79. "totalPrice" : 290,
  80. "averageQuantity" : 8.6,
  81. "count" : 6
  82. }
  83. #三、使用$group按item字段对文档进行分组(不展示其他字段)
  84. db.mycollection.aggregate(
  85. [
  86. {
  87. $group:{
  88. _id:"$item"
  89. }
  90. }
  91. ]
  92. )
  93. { "_id" : null }
  94. { "_id" : "xyz" }
  95. { "_id" : "jkl" }
  96. { "_id" : "abc" }
  97. #四、数据转换——将集合中的数据按price分组转换成item数组(items可以自定义,是分组后的列表)
  98. db.mycollection.aggregate(
  99. [
  100. {
  101. $group:{
  102. _id:"$price",
  103. items:{$push:"$item"}
  104. }
  105. }
  106. ]
  107. )
  108. { "_id" : null, "items" : [ ] }
  109. { "_id" : 5, "items" : [ "xyz", "xyz" ] }
  110. { "_id" : 20, "items" : [ "jkl" ] }
  111. { "_id" : 10, "items" : [ "abc", "abc" ] }
  112. 注:效果就是以price进行分组,列出每个price下面都有哪些个item。
  113. #四、数据转换——以item分组,使用$$ROOT系统变量
  114. db.mycollection.aggregate(
  115. [
  116. {
  117. $group:{
  118. _id:"$item",
  119. items:{$push:"$$ROOT"}
  120. }
  121. }
  122. ]
  123. )
  124. {
  125. "_id" : "xyz",
  126. "items" : [
  127. {
  128. "_id" : 3,
  129. "item" : "xyz",
  130. "price" : 5,
  131. "quantity" : 10,
  132. "date" : ISODate("2014-03-15T09:00:00Z")
  133. },
  134. {
  135. "_id" : 4,
  136. "item" : "xyz",
  137. "price" : 5,
  138. "quantity" : 20,
  139. "date" : ISODate("2014-04-04T11:21:39.736Z")
  140. }
  141. ]
  142. }
  143. {
  144. "_id" : "jkl",
  145. "items" : [
  146. {
  147. "_id" : 2,
  148. "item" : "jkl",
  149. "price" : 20,
  150. "quantity" : 1,
  151. "date" : ISODate("2014-03-01T09:00:00Z")
  152. }
  153. ]
  154. }
  155. {
  156. "_id" : "abc",
  157. "items" : [
  158. {
  159. "_id" : 1,
  160. "item" : "abc",
  161. "price" : 10,
  162. "quantity" : 2,
  163. "date" : ISODate("2014-03-01T08:00:00Z")
  164. },
  165. {
  166. "_id" : 5,
  167. "item" : "abc",
  168. "price" : 10,
  169. "quantity" : 10,
  170. "date" : ISODate("2014-04-04T21:23:13.331Z")
  171. }
  172. ]
  173. }
  174. 注:可见系统变量$$ROOT的效果就是元素所有属性的意思

注:其实仔细对比删除查询语句和输出就知道输出的数据的结构完全是根据查询语句来的。

(3)$match

释义:过滤文档,仅将符合指定条件的文档传递到下一个管道阶段。

$match接受一个指定查询条件的文档;查询语法与读操作查询语法相同。

{ $match: { <query> } }

$match用于对文档进行筛选,之后可以在得到的文档子集上做聚合,$match可以使用除了地理空间以外的所有常规查询操作符。在实际应用中应尽可能的将$match放在管道前面的位置。这样的做的好处和明显。1)可以快速的将不需要的文档过滤掉,减少后续管道的工作量  2)在投射和分值之前执行$match,查询可以使用索引。

注意:由上面可知$match的位置和使用次数都不是定死的。举个例子,你可以先对输入文档进行一个match过滤,然后进行了一个XX操作;操作完后依然可以在使用一次match进行过滤得到最终结果。

限制:

1)不能再$match查询中使用$作为聚合管道的一部分;

2)要在$match阶段使用$text,$match阶段必须是管道的第一阶段。

3)视图不支持文本搜索。

  1. #首先插入如下记录
  2. db.mycollection.save({ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 })
  3. db.mycollection.save({ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 })
  4. db.mycollection.save({ "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 })
  5. db.mycollection.save({ "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 })
  6. db.mycollection.save({ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 })
  7. db.mycollection.save({ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 })
  8. db.mycollection.save({ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 })
  9. #一、筛选anthor是dove的记录
  10. db.mycollection.aggregate(
  11. [
  12. {
  13. $match:{author:"dave"}
  14. }
  15. ]
  16. )
  17. { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
  18. { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
  19. #二、使用$match选择要处理的文档,然后将结果输出到$group管道以计算文档的数量:
  20. db.mycollection.aggregate(
  21. [
  22. {
  23. $match:{$or:[{score:{$gt:70,$lt:90}},{views:{$gte:1000}}]}
  24. },
  25. {
  26. $group:{
  27. _id:null,
  28. count:{$sum:1}
  29. }
  30. }
  31. ]
  32. )
  33. { "_id" : null, "count" : 5 }

(4)$unwind

释义:从输出文档解构数组字段以输出每个元素的文档。简单来说就是将指定的数组拆分以其中每个元素替换原数组形成len(数组长度)个文档(看个例子就明白了)。

  1. #语法。unwind:解开松开的意思
  2. { $unwind: <field path> }
  1. #首先插入如下一条数据
  2. db.mycollection.save({ "_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"] })
  3. #执行unwind语句
  4. db.mycollection.aggregate(
  5. [
  6. {$unwind:"$sizes"}
  7. ]
  8. )
  9. #效果如下:
  10. { "_id" : 1, "item" : "ABC1", "sizes" : "S" }
  11. { "_id" : 1, "item" : "ABC1", "sizes" : "M" }
  12. { "_id" : 1, "item" : "ABC1", "sizes" : "L" }

由上图可知其效果为:每个文档与输入文档相同,只不过用sizes字段中的每个元素替换原sizes的值形成了一些列的文档。

(5)$project

释义:$project可以从文档中选择想要的字段和不想要的字段。和mongo命令行语句中的projection意思一样。

  1. #语法
  2. { $project: { <specification(s)> } }

$project管道符的作用是选择字段、重命名字段、派生字段。

specifications有如下形式:

<field>:<1/0 or true、false> 是否包含该字段。1/true表示选择,0/false表示不选择子field。

  1. #数据依然采用前一个延时的数据
  2. #一、通过$project stage仅输出文档的author/views字段,不输出_id字段。
  3. db.mycollection.aggregate(
  4. [
  5. {$project:{author:1,views:1,_id:0}}
  6. ]
  7. )
  8. { "author" : "dave", "views" : 100 }
  9. { "author" : "dave", "views" : 521 }
  10. { "author" : "ahn", "views" : 1000 }
  11. { "author" : "li", "views" : 5000 }
  12. { "author" : "annT", "views" : 50 }
  13. { "author" : "li", "views" : 999 }
  14. { "author" : "ty", "views" : 1000 }

(6)$limit

释义:限制传到管道中下一阶段的文档数

  1. #语法
  2. { $limit: <positive integer> }
  1. #同样采用前面已经存在数据
  2. #仅选择3条记录
  3. db.mycollection.aggregate(
  4. [
  5. {$limit:3}
  6. ]
  7. )
  8. { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
  9. { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
  10. { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }

注意:

(7)$skip

释义:跳过进入stage的指定数量的文档,将其余的文档传递到管道中的下一个阶段。

  1. #语法
  2. { $skip: <positive integer> }
  1. #依然采用上面的数据
  2. #首先输出全部数据
  3. db.mycollection.aggregate()
  4. { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
  5. { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
  6. { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
  7. { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
  8. { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
  9. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
  10. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
  11. #skip前四条数据
  12. db.mycollection.aggregate(
  13. [
  14. {$skip:4}
  15. ]
  16. )
  17. { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
  18. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
  19. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }

(8)$sort

释义:对所有输入文档进行排序,并按排序顺序将他们返回到管道。

  1. #语法
  2. { $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

$sort指定要排序的字段和相应的排序顺序。<sort order>取值如下:

1) 1 指定升序

2) -1 指定降序

3) {$mata:"textScore"} 按照降序排序计算出的textScore数据。

  1. #依然采用上面的测试数据
  2. #一、score第一优先级升序排序;views第二优先级升序。
  3. db.mycollection.aggregate(
  4. [
  5. {$sort:{score:1,views:1}}
  6. ]
  7. )
  8. { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
  9. { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
  10. { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
  11. { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
  12. { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
  13. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
  14. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
  15. #二、views第一优先级升序排序;score第二优先级升序。
  16. db.mycollection.aggregate(
  17. [
  18. {$sort:{views:1,score:1}}
  19. ]
  20. )
  21. { "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
  22. { "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 }
  23. { "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
  24. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
  25. { "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
  26. { "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }
  27. { "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }

注:由上述事例可知$sort后面出现的各个属性的顺序就是就是排序的优先级。

(9)$sortByCount

释义:根据指定表达式的值对传入的文档分组,然后计算每个不同组中文档的数量。

应用举例二:

  1. #首先向mongodb中插入以下记录
  2. db.mycollection.save({ "_id" : 1, "cust_id" : "abc1", "ord_date" : ISODate("2012-11-02T17:04:11.102Z"), "status" : "A", "amount" : 50 })
  3. db.mycollection.save({ "_id" : 2, "cust_id" : "xyz1", "ord_date" : ISODate("2013-10-01T17:04:11.102Z"), "status" : "A", "amount" : 100 })
  4. db.mycollection.save({ "_id" : 3, "cust_id" : "xyz1", "ord_date" : ISODate("2013-10-12T17:04:11.102Z"), "status" : "D", "amount" : 25 })
  5. db.mycollection.save({ "_id" : 4, "cust_id" : "xyz1", "ord_date" : ISODate("2013-10-11T17:04:11.102Z"), "status" : "D", "amount" : 125 })
  6. db.mycollection.save({ "_id" : 5, "cust_id" : "abc1", "ord_date" : ISODate("2013-11-12T17:04:11.102Z"), "status" : "A", "amount" : 25 })
  7. 1、统计所有数量amount的和
  8. db.getCollection("mycollection").aggregate(
  9. [
  10. {
  11. $group: {
  12. _id: null,
  13. count: {$sum: "$amount"}
  14. }
  15. }
  16. ]
  17. )
  18. { "_id" : null, "count" : 325 }
  19. 2、对每个cust_id统计数量amount和
  20. db.getCollection("mycollection").aggregate(
  21. [
  22. {
  23. $group: {
  24. _id: "$cust_id",
  25. count: {$sum: "$amount"}
  26. }
  27. }
  28. ]
  29. )
  30. { "_id" : "xyz1", "count" : 250 }
  31. { "_id" : "abc1", "count" : 75 }
  32. 3、对每一个唯一的cust_id和ord_date对进行分组,计算amount总和
  33. (1)根据cust_id和年份唯一对进行分组
  34. db.getCollection("mycollection").aggregate(
  35. [
  36. {
  37. $group: {
  38. _id: {
  39. cust_id: "$cust_id",
  40. ord_date:{
  41. year: {$year: "$ord_date"}
  42. }
  43. },
  44. count: {$sum: "$amount"}
  45. }
  46. }
  47. ]
  48. )
  49. { "_id" : { "cust_id" : "abc1", "ord_date" : { "year" : 2013 } }, "count" : 25 }
  50. { "_id" : { "cust_id" : "xyz1", "ord_date" : { "year" : 2013 } }, "count" : 250 }
  51. { "_id" : { "cust_id" : "abc1", "ord_date" : { "year" : 2012 } }, "count" : 50 }
  52. (2)根据cust_id+年份+月份唯一对进行分组
  53. db.getCollection("mycollection").aggregate(
  54. [
  55. {
  56. $group: {
  57. _id: {
  58. cust_id: "$cust_id",
  59. ord_date:{
  60. year: {$year: "$ord_date"},
  61. month: {$month: "$ord_date"}
  62. }
  63. },
  64. count: {$sum: "$amount"}
  65. }
  66. }
  67. ]
  68. )
  69. { "_id" : { "cust_id" : "xyz1", "ord_date" : { "year" : 2013, "month" : 10 } }, "count" : 250 }
  70. { "_id" : { "cust_id" : "abc1", "ord_date" : { "year" : 2013, "month" : 11 } }, "count" : 25 }
  71. { "_id" : { "cust_id" : "abc1", "ord_date" : { "year" : 2012, "month" : 11 } }, "count" : 50 }
  72. (3)根据cust_id+年份+月份+天唯一对进行分组
  73. db.getCollection("mycollection").aggregate(
  74. [
  75. {
  76. $group: {
  77. _id: {
  78. cust_id: "$cust_id",
  79. ord_date:{
  80. year: {$year: "$ord_date"},
  81. month: {$month: "$ord_date"},
  82. day: {$dayOfMonth: "$ord_date"}
  83. }
  84. },
  85. count: {$sum: "$amount"}
  86. }
  87. }
  88. ]
  89. )
  90. { "_id" : { "cust_id" : "abc1", "ord_date" : { "year" : 2013, "month" : 11, "day" : 12 } }, "count" : 25 }
  91. { "_id" : { "cust_id" : "abc1", "ord_date" : { "year" : 2012, "month" : 11, "day" : 2 } }, "count" : 50 }
  92. { "_id" : { "cust_id" : "xyz1", "ord_date" : { "year" : 2013, "month" : 10, "day" : 11 } }, "count" : 125 }
  93. { "_id" : { "cust_id" : "xyz1", "ord_date" : { "year" : 2013, "month" : 10, "day" : 1 } }, "count" : 100 }
  94. { "_id" : { "cust_id" : "xyz1", "ord_date" : { "year" : 2013, "month" : 10, "day" : 12 } }, "count" : 25 }
  95. 4、根据cust_id进行分组统计其数量后 刷选数其中数量大于2的统计结果
  96. (1)没过滤的结果
  97. db.getCollection("mycollection").aggregate(
  98. [
  99. {
  100. $group:{
  101. _id:"$cust_id",
  102. count:{$sum:1}
  103. }
  104. }
  105. ]
  106. )
  107. (2)过滤后的效果
  108. db.getCollection("mycollection").aggregate(
  109. [
  110. {
  111. $group:{
  112. _id:"$cust_id",
  113. count:{$sum:1}
  114. }
  115. },
  116. {
  117. $match:{
  118. count: {$gt: 2}
  119. }
  120. }
  121. ]
  122. )
  123. 5、对每个cust_id、年份唯一分组 计算数量总和;然后仅仅返回累加和大于25的记录。
  124. (1)没有过滤的效果
  125. db.getCollection("mycollection").aggregate(
  126. [
  127. {
  128. $group:{
  129. _id:{
  130. cust_id: "$cust_id",
  131. year: {$year: "$ord_date"}
  132. },
  133. total:{$sum:"$amount"}
  134. }
  135. }
  136. ]
  137. )
  138. { "_id" : { "cust_id" : "abc1", "year" : 2013 }, "total" : 25 }
  139. { "_id" : { "cust_id" : "xyz1", "year" : 2013 }, "total" : 250 }
  140. { "_id" : { "cust_id" : "abc1", "year" : 2012 }, "total" : 50 }
  141. (2)有过滤的效果
  142. db.getCollection("mycollection").aggregate(
  143. [
  144. {
  145. $group:{
  146. _id:{
  147. cust_id: "$cust_id",
  148. year: {$year: "$ord_date"}
  149. },
  150. total:{$sum:"$amount"}
  151. }
  152. },
  153. {
  154. $match:{
  155. total: {$gt: 25}
  156. }
  157. }
  158. ]
  159. )
  160. { "_id" : { "cust_id" : "xyz1", "year" : 2013 }, "total" : 250 }
  161. { "_id" : { "cust_id" : "abc1", "year" : 2012 }, "total" : 50 }
  162. 6、对status=A的记录根据cust_id分组计算amount总和
  163. (1)未根据status过滤的效果如下
  164. db.getCollection("mycollection").aggregate(
  165. [
  166. {
  167. $group:{
  168. _id: {
  169. cust_id: "$cust_id"
  170. },
  171. total: {$sum: "$amount"}
  172. }
  173. }
  174. ]
  175. )
  176. { "_id" : { "cust_id" : "xyz1" }, "total" : 250 }
  177. { "_id" : { "cust_id" : "abc1" }, "total" : 75 }
  178. (2)根据status过滤的效果如下
  179. //这里的_id直接写成 "_id: "$cust_id","也是可以的
  180. db.getCollection("mycollection").aggregate(
  181. [
  182. {
  183. $match:{
  184. status: {$eq: "A"}
  185. }
  186. },
  187. {
  188. $group:{
  189. _id: {
  190. cust_id: "$cust_id"
  191. },
  192. total: {$sum: "$amount"}
  193. }
  194. }
  195. ]
  196. )
  197. { "_id" : { "cust_id" : "xyz1" }, "total" : 100 }
  198. { "_id" : { "cust_id" : "abc1" }, "total" : 75 }
  199. 7、对status=A的记录根据cust_id分组计算amount总和 仅展示总数大于等于100的记录
  200. /*
  201. 就是说match等这些可以有多个,根据自然语义排布就是可以的。
  202. */
  203. db.getCollection("mycollection").aggregate(
  204. [
  205. {
  206. $match:{
  207. status: {$eq: "A"}
  208. }
  209. },
  210. {
  211. $group:{
  212. _id: {
  213. cust_id: "$cust_id"
  214. },
  215. total: {$sum: "$amount"}
  216. }
  217. },
  218. {
  219. $match:{
  220. total:{$gte:100}
  221. }
  222. }
  223. ]
  224. )
  225. { "_id" : { "cust_id" : "xyz1" }, "total" : 100 }

对一个集合的数据按照kfuin进行分组:

  1. db.collection_event_track_649.aggregate(
  2. [
  3. {
  4. "$group" : {
  5. "_id":"$kfuin",
  6. "count" : {
  7. "$sum" : 1
  8. }
  9. }
  10. }
  11. ]
  12. )

2、MapReduce

MapReduce:mapreduce是一种编程模型,用于大规模数据集的并行运算。MapReduce采用分而治之的思想,简单来说就是:将待处理的大数据集分为若干个小数据集,对各个小数据集进行计算获取中间结果,最后整合中间结果获取最终的结果。mongodb也实现了mapreduce,用法还是比较简单的,语法如下:

  1. db.collection.mapReduce(
  2. function() {emit(key,value);}, //map 函数
  3. function(key,values) {return reduceFunction}, //reduce 函数
  4. {
  5. out: collection, //输出
  6. query: document, //查询条件,在Map函数执行前过滤出符合条件的documents
  7. sort: document, //再Map前对documents进行排序
  8. limit: number //发送给map函数的document条数
  9. }
  10. )

mongoDB的MapReduce可以简单分为两个阶段:

Map阶段:

  栗子中的map函数为 function() {emit(this.cust_id,this.amount)} ,执行map函数前先进行query过滤,找到 status=A 的documents,然后将过滤后的documents发送给map函数,map函数遍历documents,将cust_id作为key,amount作为value,如果一个cust_id有多个amount值时,value为数组[amount1,amount2..],栗子的map函数获取的key/value集合中有两个key/value对: {“A123”:[500,250]}和{“B212”:200} 

Reduce阶段:

  reduce函数封装了我们的聚合逻辑,reduce函数会逐个计算map函数传过去的key/value对,在上边栗子中的reduce函数的目的是计算amount的总和。

  上边栗子最终结果存放在集合myresultColl中(out的作用),通过命令 db.myresultColl.find() 查看结果如下:

  1. [
  2. {
  3. "_id" : "A123",
  4. "value" : 750
  5. },
  6. {
  7. "_id" : "B212",
  8. "value" : 200
  9. }
  10. ]

   MapReduce属于轻量级的集合框架,没有聚合管道那么多运算符,聚合的思路也比较简单:先写一个map函数用于确定需要整合的key/value对(就是我们感兴趣的字段),然后写一个reduce函数,其内部封装着我们的聚合逻辑,reduce函数会逐一处理map函数获取的key/value对,以此获取聚合结果。

小结

  本节通过几个栗子简单介绍了mongoDB的聚合框架:集合管道和MapReduce,聚合管道提供了十分丰富的运算符,让我们可以方便地进行各种聚合操作,因为聚合管道使用的是mongoDB内置函数所以计算效率一般不会太差。需要注意:①管道处理结果会放在一个document中,所以处理结果不能超过16M(Bson格式的最大尺寸),②聚合过程中内存不能超过100M(可以通过设置{“allowDiskUse”: True}来解决);MapReduce的map函数和reduce函数都是我们自定义的js函数,这种聚合方式比聚合管道更加灵活,我们可以通过编写js代码来处理复杂的聚合任务,MapReduce的缺点是聚合的逻辑需要我们自己编码实现。综上,对于一些简单的固定的聚集操作推荐使用聚合管道,对于一些复杂的、大量数据集的聚合任务推荐使用MapReduce。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/390827
推荐阅读
相关标签
  

闽ICP备14008679号