新聞中心

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


咨詢
建站咨詢
