集合中的数据有
/* 1 */
{
"_id" : ObjectId("5b496ee9497e95fe15e0b935"),
"name" : "李四",
"age" : NumberLong(12),
"major" : "math",
"address" : "北京海淀区"
}
/* 2 */
{
"_id" : ObjectId("5b4995e824cd61fde7f581ee"),
"name" : "张三",
"age" : NumberLong(18),
"address" : "纽约"
}
/* 3 */
{
"_id" : ObjectId("5b497360497e95fe15e0b93e"),
"name" : "bingo",
"age" : NumberLong(19),
"major" : "computer",
"address" : "上海"
}
/* 4 */
{
"_id" : ObjectId("5b496f81497e95fe15e0b936"),
"name" : "haha",
"age" : NumberLong(21),
"major" : "chinese",
"address" : "天津"
}
/* 5 */
{
"_id" : ObjectId("5b499bf37c9fa794f8424f58"),
"name" : "yuyuyu",
"age" : NumberLong(40),
"address" : "纽约"
}
/* 6 */
{
"_id" : ObjectId("5b4995c77c9fa794f8424f57"),
"name" : "张三",
"age" : "18",
"address" : "纽约"
}
/* 7 */
{
"_id" : ObjectId("5b496f9d497e95fe15e0b937"),
"name" : "sank",
"age" : "22",
"major" : "computer",
"address" : "天津"
}
聚合基本语法
db.getCollection("test").aggregate(option)
count操作:
db.getCollection("test").count(<query>)聚合写法:
db.getCollection("test").aggregate({$group:{_id:null,count:{$sum:1}}})
分组count:
db.getCollection("test").aggregate({$group:{_id:"$address",count:{$sum:1}}})
分组sum:
db.getCollection("test").aggregate({$group:{_id:"$address",sumage:{$sum:"$age"}}})
分组avg:
db.getCollection("test").aggregate({$group:{_id:"$address",avg:{$avg:"$age"}}})
常见的聚合表达式表达式 描述 实例
$sum 计算总和。 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$sum : "$age"}}}])
$avg 计算平均值 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$avg : "$age"}}}])
$min 获取集合中所有文档对应值得最小值。 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$min : "$age"}}}])
$max 获取集合中所有文档对应值得最大值。 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$max : "$age"}}}])
$push 在结果文档中插入值到一个数组中。 db.mycol.aggregate([{$group : {_id : "$name", url : {$push: "$url"}}}])
$addToSet 在结果文档中插入值到一个数组中,但不创建副本。 db.mycol.aggregate([{$group : {_id : "$name", url : {$addToSet : "$url"}}}])
$first 根据资源文档的排序获取第一个文档数据。 db.mycol.aggregate([{$group : {_id : "$name", first_url : {$first : "$url"}}}])
$last 根据资源文档的排序获取最后一个文档数据 db.mycol.aggregate([{$group : {_id : "$name", last_url : {$last : "$url"}}}])
管道概念,MongoDB聚合管道在一个管道处理后交给下一个管道,下面是常用的管道操作: $project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
$match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
$limit:用来限制MongoDB聚合管道返回的文档数。
$skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
$group:将集合中的文档分组,可用于统计结果。
$sort:将输入文档排序后输出。
$geoNear:输出接近某一地理位置的有序文档。
示例:
db.getCollection("test").aggregate({$project:{name:1,age:1}})返回值取name和age聚合match 后 group
db.getCollection("test").aggregate({$match:{age:{$gt:10,$lte:20}}},{$group:{_id:"$address",avg:{$avg:"$age"}}})
筛选后分组
相当于where sth group by sth
db.getCollection("test").aggregate({$group:{_id:"$address",avg:{$avg:"$age"}}},{$match:{avg:{$gt:10,$lte:20}}})
分组后筛选
相当于 group by sth having sth
db.getCollection("test").aggregate(
{$group:{_id:"$address",avg:{$avg:"$age"}}},
{$match:{avg:{$gt:10,$lte:20}}},
{$sort:{avg:1}}
)
分组后筛选再排序
相当于group by sth having sth order by sth
/* 1 */
{
"_id" : ObjectId("5b496ee9497e95fe15e0b935"),
"name" : "李四",
"age" : NumberLong(12),
"major" : "math",
"address" : "北京海淀区"
}
/* 2 */
{
"_id" : ObjectId("5b4995e824cd61fde7f581ee"),
"name" : "张三",
"age" : NumberLong(18),
"address" : "纽约"
}
/* 3 */
{
"_id" : ObjectId("5b497360497e95fe15e0b93e"),
"name" : "bingo",
"age" : NumberLong(19),
"major" : "computer",
"address" : "上海"
}
/* 4 */
{
"_id" : ObjectId("5b496f81497e95fe15e0b936"),
"name" : "haha",
"age" : NumberLong(21),
"major" : "chinese",
"address" : "天津"
}
/* 5 */
{
"_id" : ObjectId("5b499bf37c9fa794f8424f58"),
"name" : "yuyuyu",
"age" : NumberLong(40),
"address" : "纽约"
}
/* 6 */
{
"_id" : ObjectId("5b4995c77c9fa794f8424f57"),
"name" : "张三",
"age" : "18",
"address" : "纽约"
}
/* 7 */
{
"_id" : ObjectId("5b496f9d497e95fe15e0b937"),
"name" : "sank",
"age" : "22",
"major" : "computer",
"address" : "天津"
}
聚合基本语法
db.getCollection("test").aggregate(option)
count操作:
db.getCollection("test").count(<query>)聚合写法:
db.getCollection("test").aggregate({$group:{_id:null,count:{$sum:1}}})
分组count:
db.getCollection("test").aggregate({$group:{_id:"$address",count:{$sum:1}}})
分组sum:
db.getCollection("test").aggregate({$group:{_id:"$address",sumage:{$sum:"$age"}}})
分组avg:
db.getCollection("test").aggregate({$group:{_id:"$address",avg:{$avg:"$age"}}})
常见的聚合表达式表达式 描述 实例
$sum 计算总和。 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$sum : "$age"}}}])
$avg 计算平均值 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$avg : "$age"}}}])
$min 获取集合中所有文档对应值得最小值。 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$min : "$age"}}}])
$max 获取集合中所有文档对应值得最大值。 db.mycol.aggregate([{$group : {_id : "$name", num_tutorial : {$max : "$age"}}}])
$push 在结果文档中插入值到一个数组中。 db.mycol.aggregate([{$group : {_id : "$name", url : {$push: "$url"}}}])
$addToSet 在结果文档中插入值到一个数组中,但不创建副本。 db.mycol.aggregate([{$group : {_id : "$name", url : {$addToSet : "$url"}}}])
$first 根据资源文档的排序获取第一个文档数据。 db.mycol.aggregate([{$group : {_id : "$name", first_url : {$first : "$url"}}}])
$last 根据资源文档的排序获取最后一个文档数据 db.mycol.aggregate([{$group : {_id : "$name", last_url : {$last : "$url"}}}])
管道概念,MongoDB聚合管道在一个管道处理后交给下一个管道,下面是常用的管道操作: $project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
$match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
$limit:用来限制MongoDB聚合管道返回的文档数。
$skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
$unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
$group:将集合中的文档分组,可用于统计结果。
$sort:将输入文档排序后输出。
$geoNear:输出接近某一地理位置的有序文档。
示例:
db.getCollection("test").aggregate({$project:{name:1,age:1}})返回值取name和age聚合match 后 group
db.getCollection("test").aggregate({$match:{age:{$gt:10,$lte:20}}},{$group:{_id:"$address",avg:{$avg:"$age"}}})
筛选后分组
相当于where sth group by sth
db.getCollection("test").aggregate({$group:{_id:"$address",avg:{$avg:"$age"}}},{$match:{avg:{$gt:10,$lte:20}}})
分组后筛选
相当于 group by sth having sth
db.getCollection("test").aggregate(
{$group:{_id:"$address",avg:{$avg:"$age"}}},
{$match:{avg:{$gt:10,$lte:20}}},
{$sort:{avg:1}}
)
分组后筛选再排序
相当于group by sth having sth order by sth