mongodb group php 操作

 

 

紧接着上篇来,这篇主要讲,mongodb的group功能,做的还是挺强大的,相当对于find(),skip(),distinct()等,用法比较复杂。

测试数据

  1. > db.fruit.find();  
  2. "_id" : 1, "category" : "fruit", "name" : "apple" }  
  3. "_id" : 2, "category" : "fruit", "name" : "peach" }  
  4. "_id" : 3, "category" : "fruit", "name" : "banana" }  
  5. "_id" : 4, "category" : "veggie", "name" : "corn" }  
  6. "_id" : 5, "category" : "veggie", "name" : "broccoli" }  

1,根据category分组

  1. > db.fruit.group(  
  2.        {  
  3.          key: { category: 1},  
  4.          reduce: function(obj, prev) {  
  5.                      prev.items.push(obj.name);  
  6.                  },  
  7.          initial: { items : [] }  
  8.        }  
  9.     );  
  10. [  
  11.         {  
  12.                 "category" : "fruit",  
  13.                 "items" : [  
  14.                         "apple",  
  15.                         "peach",  
  16.                         "banana"  
  17.                 ]  
  18.         },  
  19.         {  
  20.                 "category" : "veggie",  
  21.                 "items" : [  
  22.                         "corn",  
  23.                         "broccoli"  
  24.                 ]  
  25.         }  
  26. ]  

php代码如下

  1. $keys = array("category" => 1);  
  2. $initial = array("items" => array());  
  3. $reduce = "function (obj, prev) { prev.items.push(obj.name); }";  
  4. $g = $collection->group($keys, $initial, $reduce);  
  5.   
  6. print_r($g);   //结果如下。  
  7.   
  8. Array  
  9. (  
  10.     [retval] => Array  
  11.         (  
  12.             [0] => Array  
  13.                 (  
  14.                     [category] => fruit  
  15.                     [items] => Array  
  16.                         (  
  17.                             [0] => apple  
  18.                             [1] => peach  
  19.                             [2] => banana  
  20.                         )  
  21.   
  22.                 )  
  23.   
  24.             [1] => Array  
  25.                 (  
  26.                     [category] => veggie  
  27.                     [items] => Array  
  28.                         (  
  29.                             [0] => corn  
  30.                             [1] => broccoli  
  31.                         )  
  32.   
  33.                 )  
  34.   
  35.         )  
  36.   
  37.     [count] => 5  
  38.     [keys] => 2  
  39.     [ok] => 1  
  40. )  

2,根据category来分组,并统计count

  1. > db.fruit.group(  
  2.            {  
  3.              key: { category: 1},  
  4.              cond: { _id: { $gt: 2 } },  
  5.              reduce: function(obj, prev) {  
  6.                 prev.items.push(obj.name);  
  7.                 prev.count++;  
  8.              },  
  9.              initial: { items : [] ,count:0}  
  10.            }  
  11.         );  
  12. [  
  13.     {  
  14.         "category" : "fruit",  
  15.         "items" : [  
  16.             "banana"  
  17.         ],  
  18.         "count" : 1  
  19.     },  
  20.     {  
  21.         "category" : "veggie",  
  22.         "items" : [  
  23.             "corn",  
  24.             "broccoli"  
  25.         ],  
  26.         "count" : 2  
  27.     }  
  28. ]  

php代码如下:

  1. $keys = array("category" => 1);  
  2. $initial = array("items" => array(),'count'=>0);  
  3. $reduce = "function (obj, prev) { " .  
  4.               "prev.items.push(obj.name); " .  
  5.               "prev.count++;" .  
  6.           "}";  
  7. $condition = array('condition' => array("_id" => array( '$gt' => 2)));  
  8. $g = $collection->group($keys, $initial, $reduce, $condition);  
  9.   
  10. print_r($g);   //结果如下。  
  11.   
  12. Array  
  13. (  
  14.     [retval] => Array  
  15.         (  
  16.             [0] => Array  
  17.                 (  
  18.                     [category] => fruit  
  19.                     [items] => Array  
  20.                         (  
  21.                             [0] => banana  
  22.                         )  
  23.   
  24.                     [count] => 1  
  25.                 )  
  26.   
  27.             [1] => Array  
  28.                 (  
  29.                     [category] => veggie  
  30.                     [items] => Array  
  31.                         (  
  32.                             [0] => corn  
  33.                             [1] => broccoli  
  34.                         )  
  35.   
  36.                     [count] => 2  
  37.                 )  
  38.         )  
  39.   
  40.     [count] => 3  
  41.     [keys] => 2  
  42.     [ok] => 1  
  43. )  

3,利用aggregate group功能,也挺强大

  1. > db.fruit.aggregate([  
  2.                      { $match: { _id: {$gt:0} } },  
  3.                      { $group: { _id: "$category", count: { $sum: 1 } } },  
  4.                      { $sort: { count: -1 } }  
  5.                    ]);  
  6. "_id" : "fruit", "count" : 3 }  
  7. "_id" : "veggie", "count" : 2 }  

php代码如下:

  1. $cond = array(  
  2.     array(  
  3.         '$match' => array('_id' => array('$gt' => 0)),  
  4.     ),  
  5.     array(  
  6.         '$group' => array(  
  7.             '_id' => '$category',  
  8.            'count' => array('$sum' => 1),  
  9.         ),  
  10.     ),  
  11.     array(  
  12.         '$sort' => array("count" => -1),  
  13.     ),  
  14. );  
  15. $result = $collection->aggregate($cond);  
  16. print_r($result);    //结果如下:  
  17.   
  18. Array  
  19. (  
  20.     [result] => Array  
  21.         (  
  22.             [0] => Array  
  23.                 (  
  24.                     [_id] => fruit  
  25.                     [count] => 3  
  26.                 )  
  27.   
  28.             [1] => Array  
  29.                 (  
  30.                     [_id] => veggie  
  31.                     [count] => 2  
  32.                 )  
  33.   
  34.         )  
  35.   
  36.     [ok] => 1  
  37. )  

mongodb 的select 操作有很多,在这里,只是说了一些常用的功能。

原文地址:https://www.cnblogs.com/shijiaoyun/p/5176913.html