(譯文)SQL與Mongodb彙總之前的對應關係

來源:互聯網
上載者:User

標籤:match   status   price   參考   log   group by   例子   彙總函式   ref   

本文翻譯自:https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/

由於本人也在學習Mongodb,項目中用到彙總,看到文檔這篇不錯就翻譯一下(僅供參考)

SQL中的彙總函式和Mongodb中的管道相互對應的關係:

WHERE             $matchGROUP BY     $groupHAVING             $matchSELECT             $projectORDER BY     $sortLIMIT             $limitSUM()             $sumCOUNT()             $sumjoin             $lookup

例子:

先建立文檔,填充資料

/* 0 */{  "_id" : ObjectId("5812b447311bb4272016496a"),  "cust_id" : "abc123",  "ord_date" : ISODate("2012-11-02T17:04:11.102Z"),  "status" : "A",  "price" : 50,  "items" : [{      "sku" : "xxx",      "qty" : 25,      "price" : 1    }, {      "sku" : "yyy",      "qty" : 25,      "price" : 1    }]}/* 1 */{  "_id" : ObjectId("58131494311bb418b058fcba"),  "cust_id" : "a",  "ord_date" : ISODate("2012-11-02T17:04:11.102Z"),  "status" : "B",  "price" : 70,  "items" : [{      "sku" : "xxx",      "qty" : 25,      "price" : 1    }, {      "sku" : "yyy",      "qty" : 25,      "price" : 1    }]}/* 2 */{  "_id" : ObjectId("581314b6311bb418b058fcbb"),  "cust_id" : "ab",  "ord_date" : ISODate("2012-11-02T17:04:11.102Z"),  "status" : "E",  "price" : 60,  "items" : [{      "sku" : "xxx",      "qty" : 55,      "price" : 1    }, {      "sku" : "yyy",      "qty" : 25,      "price" : 1    }]}

例1:

SQL:                                                               

SELECT COUNT(*) AS count  FROM orders

  

Mongodb:

db.orders.aggregate([  {    $group:{          _id:null,          count:{$sum:1}        }  }])

 

例2:

SQL:

SELECT SUM(price) AS total FROM orders

Mongodb:

db.orders.aggregate(         [          {            $group:            {            _id:null,            total:{$sum:"$price"}            }          }        ])

例3:

SQL:

SELECT cust_id,SUM(price) AS total FROM orders GROUP BY cust_id

Mongodb:

db.orders.aggregate([            {              $group:                  {                        _id:"$cust_id",                    total:                      {                       $sum:"$price"                      }                  }            },            {              $sort:                {                  total:1                }            }          ])         

例4:

SQL:

SELECT cust_id, ord_date,SUM(price) AS total  FROM orders  GROUP BY cust_id, ord_date

Mongodb:

db.orders.aggregate([{    $group:    {      _id:     {       cust_id:"$cust_id",       ord_date:            {            month:{$month:"$ord_date"},            day:{$dayOfMonth:"$ord_date"},            year:{$year:"$ord_date"}            }      },      total:{$sum:"$price"}     }}])

 

     

 

(譯文)SQL與Mongodb彙總之前的對應關係

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.