MongoDB:The Definitive Guide 2nd笔记(二)
2016-09-10 08:50
309 查看
Grouping operators
sum
avg
max min first last
addToSet
push
Grouping behavior
unwind
“max"and"min” look through each document and find the extreme values.
Alternatively, “first"and"last” return useful results when your data is sorted by the fields you are looking for.
如果数据是已经排序好了,就用
This means that, with sharding, $group will first be run on each shard and then the individual shards’ groups will be sent to the mongos to do the final grouping and the remainder of the pipeline will be run on the mongos (not the shards).
利用
sum
avg
max min first last
addToSet
push
Grouping behavior
unwind
Grouping operators
$sum
"$sum" : value //计算每个文档的value的总和,value也可以是更加复杂的表达式 > db.sales.aggregate( { "$group" : { "_id" : "$country", "totalRevenue" : {"$sum" : "$revenue"} } })
$avg
"$avg" : value //计算value的均值 > db.sales.aggregate( { "$group" : { "_id" : "$country", "totalRevenue" : {"$average" : "$revenue"}, "numSales" : {"$sum" : 1} } })
max min first last
"$max" : expr "$min" : expr "$first" : expr "$last" : expr
“max"and"min” look through each document and find the extreme values.
$max和
$min搜索每一个文档找到最大值和最小值
db.scores.aggregate( { "$group" : { "_id" : "$grade", "lowestScore" : {"$min" : "$score"}, "highestScore" : {"$max" : "$score"} } })
Alternatively, “first"and"last” return useful results when your data is sorted by the fields you are looking for.
如果数据是已经排序好了,就用
$first和
$last,比使用
$min和
$max更加有效。如果数据并没有排序或没有打算排序,使用
$min和
$max更加有效
$addToSet
"$addToSet" : expr 把当前遇到的数据添加到数组中,如果expr不在数组中,就添加进去。 在结果数组中每个value只出现一次。
$push
"$push" : expr 把当前遇到的值都加到数组中,返回一个数组包含所有值。
Grouping behavior
$group must collect all documents, split them into groups, then send them to the next operator in the pipeline.This means that, with sharding, $group will first be run on each shard and then the individual shards’ groups will be sent to the mongos to do the final grouping and the remainder of the pipeline will be run on the mongos (not the shards).
$unwind
Unwinding 把数组中的每一个元素分到单独的文档中,举例如下://每条博客可能有多个评论 db.blog.findOne() { "_id" : ObjectId("50eeffc4c82a5271290530be"), "author" : "k", "post" : "Hello, world!", "comments" : [ { "author" : "mark", "date" : ISODate("2013-01-10T17:52:04.148Z"), "text" : "Nice post" }, { "author" : "bill", "date" : ISODate("2013-01-10T17:52:04.148Z"), "text" : "I agree" } ] }
利用
unwind可以得到
db.blog.aggregate({"$unwind" : "$comments"}) { "results" : { "_id" : ObjectId("50eeffc4c82a5271290530be"), "author" : "k", "post" : "Hello, world!", "comments" : { "author" : "mark", "date" : ISODate("2013-01-10T17:52:04.148Z"), "text" : "Nice post" } }, { "_id" : ObjectId("50eeffc4c82a5271290530be"), "author" : "k", "post" : "Hello, world!", "comments" : { "author" : "bill", "date" : ISODate("2013-01-10T17:52:04.148Z"), "text" : "I agree" } } ], "ok" : 1 }
相关文章推荐
- MongoDB:The Definitive Guide 2nd笔记(三)
- MongoDB:The Definitive Guide 2nd笔记之system.profile
- MongoDB:The Definitive Guide 2nd笔记之查看数据库大小
- MongoDB:The Definitive Guide 2nd笔记之Seeing the Current Operations
- MongoDB:The Definitive Guide 2nd笔记之特殊Indexes
- Hadoop The Definitive Guide 2nd Edition 笔记
- [JavaScript: The Definitive Guide]笔记(1)
- Hadoop- The Definitive Guide 笔记2
- The Definitive Guide To Django 2 学习笔记(六) 第四章 模板 (二)使用模板系统
- The Definitive Guide To Django 2 学习笔记(七) 第四章 模板 (三)使用模板系统
- Objective-C Programming: The Big Nerd Ranch Guide (2nd Edition) 阅读笔记(Part I & II)
- The Definitive Guide To Django 2 学习笔记(三) URLconfs 和松耦合
- The Definitive Guide To Django 2 学习笔记(二) 第二个View 动态内容
- JS: The Definitive Guide 笔记 - Array
- The Definitive Guide To Django 2 学习笔记(四) 动态URLs
- MongoDB:The Definitive Guide CHAPTER 2 Getting Started
- JavaScript正则表达式(JavaScript:The Definitive Guide笔记)
- MongoDB的内部构造 From 《MongoDB The Definitive Guide》
- 电子书下载:The Definitive Guide to MongoDB
- The Definitive Guide To Django 2 学习笔记(八) 第四章 模板 (四)基本的模板标签和过滤器