新聞中心

站在用戶的角度思考問題,與客戶深入溝通,找到平橋網(wǎng)站設(shè)計(jì)與平橋網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋平橋地區(qū)。
aggregate() 方法
您可以使用 MongoDB 中的 aggregate() 方法來執(zhí)行聚合操作,其語法格式如下:
db.collection_name.aggregate(aggregate_operation)
【示例】假設(shè)集合“course”中有如下數(shù)據(jù):
> db.course.find().pretty()
{
"_id" : ObjectId("60331a7eee79704753940391"),
"title" : "HTML教程",
"author" : "編程幫",
"url" : "http://www.biancheng.com/html/index.html"
}
{
"_id" : ObjectId("60331a7eee79704753940392"),
"title" : "C#教程",
"author" : "編程幫",
"url" : "http://www.biancheng.com/csharp/index.html"
}
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "編程幫",
"url" : "http://www.biancheng.com/mongodb/index.html"
} 若要統(tǒng)計(jì)每個(gè)作者“author”一共編寫了多少教程,可以使用下面的 aggregate() 方法:
> db.course.aggregate([{$group : {_id : "$author", sum : {$sum : 1}}}])
{ "_id" : "編程幫", "sum" : 3 } 上述示例類似于 SQL 語句中的SELECT author, count(*) FROM course GROUP BY author。
下表中展示了一些聚合表達(dá)式:
| 表達(dá)式 | 描述 | 實(shí)例 |
|---|---|---|
| $sum | 計(jì)算總和 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$sum : "$likes"}}}]) |
| $avg | 計(jì)算平均值 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$avg : "$likes"}}}]) |
| $min | 獲取集合中所有文檔對應(yīng)值得最小值 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$min : "$likes"}}}]) |
| $max | 獲取集合中所有文檔對應(yīng)值得最大值 | db.mycol.aggregate([{$group : {_id : "$author", num_tutorial : {$max : "$likes"}}}]) |
| $push | 在結(jié)果文檔中插入值到一個(gè)數(shù)組中 | db.mycol.aggregate([{$group : {_id : "$author", url : {$push: "$url"}}}]) |
| $addToSet | 在結(jié)果文檔中插入值到一個(gè)數(shù)組中,但不創(chuàng)建副本 | db.mycol.aggregate([{$group : {_id : "$author", url : {$addToSet : "$url"}}}]) |
| $first | 根據(jù)資源文檔的排序獲取第一個(gè)文檔數(shù)據(jù) | db.mycol.aggregate([{$group : {_id : "$author", first_url : {$first : "$url"}}}]) |
| $last | 根據(jù)資源文檔的排序獲取最后一個(gè)文檔數(shù)據(jù) | db.mycol.aggregate([{$group : {_id : "$author", last_url : {$last : "$url"}}}]) |
管道
在 UNIX 命令中,管道意味著可以將某些操作的輸出結(jié)果作為下一個(gè)命令的參數(shù),以此類推。MongoDB 中同樣也支持管道,即 MongoDB 會(huì)在一個(gè)管道處理完畢后將結(jié)果傳遞給下一個(gè)管道處理,而且管道操作是可以重復(fù)的。
下面介紹了聚合框架中幾個(gè)常用的操作:
- $project:用于從集合中選擇要輸出的字段;
- $match:用于過濾數(shù)據(jù),只輸出符合條件的文檔,可以減少作為下一階段輸入的文檔數(shù)量;
- $group:對集合中的文檔進(jìn)行分組,可用于統(tǒng)計(jì)結(jié)果;
- $sort:將輸入文檔進(jìn)行排序后輸出;
- $skip:在聚合管道中跳過指定數(shù)量的文檔,并返回余下的文檔;
- $limit:用來限制 MongoDB 聚合管道返回的文檔數(shù)量;
- $unwind:將文檔中的某一個(gè)數(shù)組類型字段拆分成多條,每條包含數(shù)組中的一個(gè)值。
下面通過幾個(gè)簡單的示例來演示 MongoDB 中管道的使用:
1) $project
【示例】使用 $project 來選擇要輸出的字段:
> db.course.aggregate({$project:{title:1, author:1}}).pretty()
{
"_id" : ObjectId("60331a7eee79704753940391"),
"title" : "HTML教程",
"author" : "編程幫"
}
{
"_id" : ObjectId("60331a7eee79704753940392"),
"title" : "C#教程",
"author" : "編程幫"
}
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "編程幫"
} 通過運(yùn)行結(jié)果可以看出,文檔中的 _id 字段默認(rèn)是選中的,如果不想顯示 _id 字段的話,可以像下面這樣:
> db.course.aggregate({$project:{_id:0, title:1, author:1}}).pretty()
{ "title" : "HTML教程", "author" : "編程幫" }
{ "title" : "C#教程", "author" : "編程幫" }
{ "title" : "MongoDB教程", "author" : "編程幫" }2) $skip
【示例】使用 $skip 跳過指定數(shù)量的文檔:
> db.course.aggregate({$skip:2}).pretty()
{
"_id" : ObjectId("60331a7eee79704753940393"),
"title" : "MongoDB教程",
"author" : "編程幫",
"url" : "http://www.biancheng.com/mongodb/index.html"
}當(dāng)前文章:MongoDB聚合查詢
標(biāo)題鏈接:http://www.dlmjj.cn/article/dhogddc.html


咨詢
建站咨詢
