<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[info] MongoDB 聚合 MongoDB 中聚合(aggregate)主要用于處理數據(諸如統計平均值,求和等),并返回計算后的數據結果。有點類似**SQL**語句中的 **count(*)**。 #### **1. 聚合方法:aggregate()** **1.1 語法:** ~~~ db.collection.aggregate(AGGREGATE_OPERATION) ~~~ **1.2 示例:** ~~~ // 集合中數據如下: { _id: ObjectId(7df78ad8902c) title: 'MongoDB Overview', description: 'MongoDB is no sql database', by_user: 'runoob.com', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { _id: ObjectId(7df78ad8902d) title: 'NoSQL Overview', description: 'No sql database is very fast', by_user: 'runoob.com', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 10 }, { _id: ObjectId(7df78ad8902e) title: 'Neo4j Overview', description: 'Neo4j is no sql database', by_user: 'Neo4j', url: 'http://www.neo4j.com', tags: ['neo4j', 'database', 'NoSQL'], likes: 750 } // 合計算每個作者所寫的文章數(select by_user, count(*) from mycol group by by_user) >db.test.aggregate([{$group : {_id : "$by_user",num_tutorial : {$sum : 1}}}]) // 用戶為:runoob.com 文章為:2條,用戶為:Neo4j 文章為:1條 { "_id" : "runoob.com", "num_tutorial" : 2 }, { "_id" : "Neo4j", "num_tutorial" : 1 } ~~~ | 表達式 | 描述 | 實例 | | --- | --- | --- | | $sum | 計算總和。 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", num\_tutorial : {$sum : "$likes"}}}\]) | | $avg | 計算平均值 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", num\_tutorial : {$avg : "$likes"}}}\]) | | $min | 獲取集合中所有文檔對應值得最小值。 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", num\_tutorial : {$min : "$likes"}}}\]) | | $max | 獲取集合中所有文檔對應值得最大值。 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", num\_tutorial : {$max : "$likes"}}}\]) | | $push | 將值加入一個數組中,不會判斷是否有重復的值。 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", url : {$push: "$url"}}}\]) | | $addToSet | 將值加入一個數組中,會判斷是否有重復的值,若相同的值在數組中已經存在了,則不加入。 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", url : {$addToSet : "$url"}}}\]) | | $first | 根據資源文檔的排序獲取第一個文檔數據。 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", first\_url : {$first : "$url"}}}\]) | | $last | 根據資源文檔的排序獲取最后一個文檔數據 | db.mycol.aggregate(\[{$group : {\_id : "$by\_user", last\_url : {$last : "$url"}}}\]) | >[info] MongoDB 管道 管道在Unix和Linux中一般用于將當前命令的輸出結果作為下一個命令的參數。 MongoDB的聚合管道將MongoDB文檔在一個管道處理完畢后將結果傳遞給下一個管道處理。管道操作是可以重復的。 ### **聚合框架中常用的幾個操作:** * $project:修改輸入文檔的結構。可以用來重命名、增加或刪除域,也可以用于創建計算結果以及嵌套文檔。 * $match:用于過濾數據,只輸出符合條件的文檔。$match使用MongoDB的標準查詢操作。 * $limit:用來限制MongoDB聚合管道返回的文檔數。 * $skip:在聚合管道中跳過指定數量的文檔,并返回余下的文檔。 * $unwind:將文檔中的某一個數組類型字段拆分成多條,每條包含數組中的一個值。 * $group:將集合中的文檔分組,可用于統計結果。 * $sort:將輸入文檔排序后輸出。 * $geoNear:輸出接近某一地理位置的有序文檔。 #### **1. $project 示例:** ``` // 只查詢 title 和 descripytion 兩個列 >db.test.aggregate({$project : {title : 1,description : 1}}) { "_id" : ObjectId("620e1d752131f8f45ef39e28"), "title" : "NoSQL Overview", "description" : "說明" } { "_id" : ObjectId("620e1de02131f8f45ef39e29"), "title" : "NoSQL Overview1", "description" : "說明1" } // 默認情況下_id字段是被包含的,如果要想不包含_id話可以這樣 >db.test.aggregate({$project : {id : 0,title : 1,description : 1}}) ``` #### **2. $match 示例:** ``` // 查詢 likes 字段 大于等于10 并且 小于等于10的數據集 >db.test.aggregate([{$match : {likes : {$gte : 10 , $lte : 10}}}]) { "_id" : ObjectId("620e1d752131f8f45ef39e28"), "title" : "NoSQL Overview", "description" : "說明", "by_user" : "wqs", "url" : "www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 10 } // 并進行分組統計結果 >db.test.aggregate([{$match : {likes : {$gte : 10 , $lte : 10}}},{$group : {_id : null , count : {$sum : 1}}}]) { "_id" : null, "count" : 1 } ``` #### **3. $skip 示例:** ``` // 過濾(跳過)第一條數據,得到了第二條數據集 >db.test.aggregate({$skip : 1}) { "_id" : ObjectId("620e1de02131f8f45ef39e29"), "title" : "NoSQL Overview1", "description" : "說明1", "by_user" : "wqs1", "url" : "www.baidu1.com", "tags" : [ "mongodb", "database", "NoSQL1" ], "likes" : 11 } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看