MongoDB权威指南
聚合
7.1-聚合框架
db.articles.arregate({“project":{"author":1}},{"$group":{"_id":"author","count":{"$sum":1}}},{"$sort":{"count":-1}},{"$limit”:5})
聚合的结果必须限制在16MB以内(MongoDB支持的最大响应消息大小)
7.2-管道操作符match
match用于对文档集合进行筛选,之后可以在筛选后的文档子集上做聚合实际使用中应该尽可能地将
match放在管道的前面位置
project使用如下方法将id从结果文档中移除db.articles.arregate(“$project”:“author”:1,”id”:0)db.articles.arregate(“$project":"newFieldName":"$originalFieldName","$sort”:“newFieldName”:1)应该尽量在修改字段名称前使用索引2−数学表达式
add
subtract
multiply
divide
mod
ex
db.employees.arregate({“project":{"totalPay":{"$add":["$salary","$bonus”]}}})
db.employees.arregate({“$project":{"totalPay":{"$subtract":[{"$add":["$salary","$bonus”]},”401k”]}}})
3-日期表达式year
month
dayOfMonth
dayOfWeek
dayOfYear
hour
minute
secondexdb.employees.arregate(“$project":"hiredIn":"$month":"$hireDate”)4−字符串表达式
substr
concat
toLower
toUpper5−逻辑表达式
cmp
strcasecmp
and
or
not
cond
ifNull
group“$group":"id":"state":"$state","city":"$city”db.employees.arregate(“$group":"id":"$country","totalRevenue":"$sum":"$revenue”)db.employees.arregate(“$group":"id":"$country","lowestScore":"$min":"$score","highestScore":"$max":"$score”)数组操作符
addToSet
push
unwind
拆分unwind可以将数组中的每个值拆分为单独的文档
sort
limit
skip使用管道MongoDB不允许单一的聚合操作占用过多的系统内存如果MongoDB发现某个聚合操作占用了207.3−MapReduce找出集合中的所有键网页分类MongoDB和MapReduce7.4−聚合命令countdistinctgroupgroup可以执行更复杂的聚合聚合7.1−聚合框架db.articles.arregate(“$project":"author":1,"$group":"id":"author","count":"$sum":1,"$sort":"count":−1,"$limit”:5)聚合的结果必须限制在16MB以内(MongoDB支持的最大响应消息大小)7.2−管道操作符
match
match用于对文档集合进行筛选,之后可以在筛选后的文档子集上做聚合实际使用中应该尽可能地将
match放在管道的前面位置
project使用如下方法将id从结果文档中移除db.articles.arregate(“$project”:“author”:1,”id”:0)db.articles.arregate(“$project":"newFieldName":"$originalFieldName","$sort”:“newFieldName”:1)应该尽量在修改字段名称前使用索引2−数学表达式
add
subtract
multiply
divide
mod
ex
db.employees.arregate({“project":{"totalPay":{"$add":["$salary","$bonus”]}}})
db.employees.arregate({“$project":{"totalPay":{"$subtract":[{"$add":["$salary","$bonus”]},”401k”]}}})
3-日期表达式year
month
dayOfMonth
dayOfWeek
dayOfYear
hour
minute
secondexdb.employees.arregate(“$project":"hiredIn":"$month":"$hireDate”)4−字符串表达式
substr
concat
toLower
toUpper5−逻辑表达式
cmp
strcasecmp
and
or
not
cond
ifNull
group“$group":"id":"state":"$state","city":"$city”db.employees.arregate(“$group":"id":"$country","totalRevenue":"$sum":"$revenue”)db.employees.arregate(“$group":"id":"$country","lowestScore":"$min":"$score","highestScore":"$max":"$score”)数组操作符
addToSet
push
unwind
拆分unwind可以将数组中的每个值拆分为单独的文档
sort
limit
$skip
使用管道
MongoDB不允许单一的聚合操作占用过多的系统内存
如果MongoDB发现某个聚合操作占用了20%以上的内存,这个操作就会直接输出错误
7.3-MapReduce
找出集合中的所有键
网页分类
MongoDB和MapReduce
7.4-聚合命令
count
distinct
group
group可以执行更复杂的聚合
MongoDB权威指南-第7章
最新推荐文章于 2025-05-22 00:16:56 发布