MongoDB 的高级查询 aggregate 聚合管道
生活随笔
收集整理的這篇文章主要介紹了
MongoDB 的高级查询 aggregate 聚合管道
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、MongoDB 聚合管道(Aggregation Pipeline)
使用聚合管道可以對集合中的文檔進行變換和組合。
實際項目:表關聯查詢、數據的統計。
MongoDB 中使用 db.COLLECTION_NAME.aggregate([{<stage>},...]) 方法 來構建和使用聚合管道。
先看下官網給的實例,感受一下聚合管道的用法。
二、MongoDB Aggregation 管道操作符與表達式
SQL 和 NOSQL 對比:
管道表達式: 管道操作符作為“鍵”,所對應的“值”叫做管道表達式。
例如{KaTeX parse error: Expected 'EOF', got '}' at position 19: …ch:{status:"A"}}?,match 稱為管道操作符,而 status:"A"稱為管道表達式, 是管道操作符的操作數(Operand)。
每個管道表達式是一個文檔結構,它是由字段名、字段值、和一些表達式操作符組成的。
三、 數據模擬
db.order.insert({"order_id":"1","uid":10,"trade_no":"111","all_price":100,"all_num":2}) db.order.insert({"order_id":"2","uid":7,"trade_no":"222","all_price":90,"all_num":2}) db.order.insert({"order_id":"3","uid":9,"trade_no":"333","all_price":20,"all_num":6}) db.order_item.insert({"order_id":"1","title":"商品鼠標 1","price":50,num:1}) db.order_item.insert({"order_id":"1","title":"商品鍵盤 2","price":50,num:1}) db.order_item.insert({"order_id":"1","title":"商品鍵盤 3","price":0,num:1}) db.order_item.insert({"order_id":"2","title":"牛奶","price":50,num:1}) db.order_item.insert({"order_id":"2","title":"酸奶","price":40,num:1}) db.order_item.insert({"order_id":"3","title":"礦泉水","price":2,num:5}) db.order_item.insert({"order_id":"3","title":"毛巾","price":10,num:1})四、 $project
修改文檔的結構,可以用來重命名、增加或刪除文檔中的字段。
要求查找 order 只返回文檔中order_id, trade_no 和 all_price 字段
五、 $match 作用用于過濾文檔。
用法類似于 find() 方法中的參數。
六、 $group
將集合中的文檔進行分組,可用于統計結果。
統計每個訂單的訂單數量,按照訂單號分組
七、 $sort
將集合中的文檔進行排序。
八、 $limit
九、 $skip
十、 $lookup 表關聯
db.order.aggregate([{$lookup: {from: "order_item",localField: "order_id",foreignField: "order_id",as: "items"} }])查詢結果:
// 1 {"_id": ObjectId("606ecfbdbb390000fa004964"),"order_id": "1","uid": 10,"trade_no": "111","all_price": 100,"all_num": 2,"items": [{"_id": ObjectId("606ecfbdbb390000fa004967"),"order_id": "1","title": "商品鼠標 1","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004968"),"order_id": "1","title": "商品鍵盤 2","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004969"),"order_id": "1","title": "商品鍵盤 3","price": 0,"num": 1}] }// 2 {"_id": ObjectId("606ecfbdbb390000fa004965"),"order_id": "2","uid": 7,"trade_no": "222","all_price": 90,"all_num": 2,"items": [{"_id": ObjectId("606ecfbdbb390000fa00496a"),"order_id": "2","title": "牛奶","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa00496b"),"order_id": "2","title": "酸奶","price": 40,"num": 1}] }// 3 {"_id": ObjectId("606ecfbdbb390000fa004966"),"order_id": "3","uid": 9,"trade_no": "333","all_price": 20,"all_num": 6,"items": [{"_id": ObjectId("606ecfbdbb390000fa00496c"),"order_id": "3","title": "礦泉水","price": 2,"num": 5},{"_id": ObjectId("606ecfbdbb390000fa00496d"),"order_id": "3","title": "毛巾","price": 10,"num": 1}] } db.order.aggregate([{$lookup: {from: "order_item",localField: "order_id",foreignField: "order_id",as: "items"} }, {$project: {trade_no: 1,all_price: 1,items: 1} }, {$match: {"all_price": {$gte: 90}} }, {$sort: {"all_price": - 1} }, ])查詢結果:
// 1 {"_id": ObjectId("606ecfbdbb390000fa004964"),"trade_no": "111","all_price": 100,"items": [{"_id": ObjectId("606ecfbdbb390000fa004967"),"order_id": "1","title": "商品鼠標 1","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004968"),"order_id": "1","title": "商品鍵盤 2","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa004969"),"order_id": "1","title": "商品鍵盤 3","price": 0,"num": 1}] }// 2 {"_id": ObjectId("606ecfbdbb390000fa004965"),"trade_no": "222","all_price": 90,"items": [{"_id": ObjectId("606ecfbdbb390000fa00496a"),"order_id": "2","title": "牛奶","price": 50,"num": 1},{"_id": ObjectId("606ecfbdbb390000fa00496b"),"order_id": "2","title": "酸奶","price": 40,"num": 1}] }總結
以上是生活随笔為你收集整理的MongoDB 的高级查询 aggregate 聚合管道的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ad19pcb设置恢复默认_条码打印机-
- 下一篇: CSS——设置第一个以外的元素样式:no