深入学习MongoDB之聚合函数

本文介绍了MongoDB中的聚合操作,包括基本语法、常用聚合表达式和管道操作等。通过具体的例子展示了如何进行分组计数、求和、平均值计算等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

集合中的数据有
/* 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值