FS1360472174 / nosql-mongo

mongo knowledge
4 stars 4 forks source link

mongo aggregation #13

Closed FS1360472174 closed 7 years ago

FS1360472174 commented 7 years ago

5

FS1360472174 commented 7 years ago

aggregation 就是轻量级的mapreduce工作 written in c++ json parameters

limit 像mapreduce一样,各个job需要使用独立的有限内存空间,mongo没有提供有效、复杂的任务调度/内存分配算法。只是简单限定每个stage最多使用100M内存,超过此值将终止计算并返回error. 为了支持较大数据collection处理,可以指定allowDiskUse,将溢出数据写入到本地的临时文件中。

http://shift-alt-ctrl.iteye.com/blog/2259216 https://docs.mongodb.com/master/reference/operator/aggregation-pipeline/

FS1360472174 commented 7 years ago

pipeline Aggregation Stages $collStats: 查看collection状态,3.4 $project: 为collection 添加新字段/重置已有字段 $match:过滤collection,不可使用where db.articles.aggregate( [ { $match : { author : "dave" } } ] ); match 与find有什么关系呢

$redact(编辑) 限制每个document的field

$limit

$skip 跳过document

$unwind document其他值不变,把数组字段拆分为array.length个

db.arrayTest.insert({"item":"1",sizes:["s","m","m"]})
db.arrayTest.insert({"item":"2",sizes:["s","m","l"]})
db.arrayTest.aggregate([{$unwind:"$sizes"}])
{ "_id" : ObjectId("583e3ccc48934084893ee8d4"), "item" : "1", "sizes" : "s" }
{ "_id" : ObjectId("583e3ccc48934084893ee8d4"), "item" : "1", "sizes" : "m" }
{ "_id" : ObjectId("583e3ccc48934084893ee8d4"), "item" : "1", "sizes" : "m" }
{ "_id" : ObjectId("583e3d1348934084893ee8d5"), "item" : "2", "sizes" : "s" }
{ "_id" : ObjectId("583e3d1348934084893ee8d5"), "item" : "2", "sizes" : "m" }
{ "_id" : ObjectId("583e3d1348934084893ee8d5"), "item" : "2", "sizes" : "l" }

$group 同SQL $sample 随机选取数据,mongo compass 对于大数据量经常用这个

$sort

db.users.aggregate(
   [
     { $sort : { age : -1, posts: 1 } }
   ]
)

$lookup 可以与相同database中的unsharded collection建立外连接查询

$out 将一定条件的document 写到新的collection,必须是pipeline的最后一步。 新的collection在pipeline 过程中不可见,如果失败,则不会创建

$indexStats index 使用情况统计 db.orders.aggregate( [ { $indexStats: { } } ] )

$facet(方面,侧面) 3.4 可以提供多个结果的合并集,比如 结果按A排序以及结果按B排序

$bucket 数据的分类 3.4

$sortByCount 3.4 $group +$sort

$addFields 3.4 类似于$project,语法稍有不同

$replaceRoot 3.4 合并,更改document的结构

$count 3.4 添加了新的属性

db.scores.aggregate(
  [
    {
      $match: {
        score: {
          $gt: 80
        }
      }
    },
    {
      $count: "passing_scores"
    }
  ]
)

{ "passing_scores" : 4 }

$graphLookup 3.4 迭代查询,from 只能用在sharded collection,可以设置深度

$graphLookup cannot use disk space as memory the way other aggregation operations can, so you must stay within the 100 megabyte memory limit.

在存储数据方面,并没有什么不同,都是reference. 可以跨越database 多个节点

FS1360472174 commented 7 years ago

$let

{ id:1 price:10 txt:0.5 discount:true }

FS1360472174 commented 7 years ago

https://www.mongodb.com/presentations/aggregation-framework-0?jmp=docs&_ga=2.92255197.657841987.1494323425-1009353054.1493262080