Next, I will talk about the group function of mongodb, which is quite powerful. It is quite complicated to use, such as find (), skip (), and distinct.
Test data
The code is as follows: |
Copy code |
> Db. fruit. find (); {"_ Id": 1, "category": "fruit", "name": "apple "} {"_ Id": 2, "category": "fruit", "name": "peach "} {"_ Id": 3, "category": "fruit", "name": "banana "} {"_ Id": 4, "category": "veggie", "name": "corn "} {"_ Id": 5, "category": "veggie", "name": "broccoli "} 1. Group by category > Db. fruit. group ( { Key: {category: 1 }, Reduce: function (obj, prev ){ Prev. items. push (obj. name ); }, Initial: {items: []} } ); [ { "Category": "fruit ", "Items ":[ "Apple ", "Peach ", "Banana" ] }, { "Category": "veggie ", "Items ":[ "Corn ", "Broccoli" ] } ] |
The php code is as follows:
The code is as follows: |
Copy code |
$ Keys = array ("category" => 1 ); $ Initial = array ("items" => array ()); $ Reduce = "function (obj, prev) {prev. items. push (obj. name );}"; $ G = $ collection-> group ($ keys, $ initial, $ reduce ); Print_r ($ g); // The result is as follows. Array ( [Retval] => Array ( [0] => Array ( [Category] => fruit [Items] => Array ( [0] => apple [1] => peach [2] => banana ) ) [1] => Array ( [Category] => veggie [Items] => Array ( [0] => corn [1] => broccoli ) ) ) [Count] => 5 [Keys] => 2 [OK] => 1 ) 2. Grouping based on category and counting count > Db. fruit. group ( { Key: {category: 1 }, Cond: {_ id: {$ gt: 2 }}, Reduce: function (obj, prev ){ Prev. items. push (obj. name ); Prev. count ++; }, Initial: {items: [], count: 0} } ); [ { "Category": "fruit ", "Items ":[ "Banana" ], "Count": 1 }, { "Category": "veggie ", "Items ":[ "Corn ", "Broccoli" ], "Count": 2 } ] |
The php code is as follows:
The code is as follows: |
Copy code |
$ Keys = array ("category" => 1 ); $ Initial = array ("items" => array (), 'count' => 0 ); $ Reduce = "function (obj, prev ){". "Prev. items. push (obj. name );". "Prev. count ++ ;". "}"; $ Condition = array ('condition '=> array ("_ id" => array (' $ gt '=> 2 ))); $ G = $ collection-> group ($ keys, $ initial, $ reduce, $ condition ); Print_r ($ g); // The result is as follows. Array ( [Retval] => Array ( [0] => Array ( [Category] => fruit [Items] => Array ( [0] => banana ) [Count] => 1 ) [1] => Array ( [Category] => veggie [Items] => Array ( [0] => corn [1] => broccoli ) [Count] => 2 ) ) [Count] => 3 [Keys] => 2 [OK] => 1 )
|
3. Using the aggregate group function is also quite powerful
The code is as follows: |
Copy code |
> Db. fruit. aggregate ([ {$ Match :{_ id :{$ gt: 0 }}}, {$ Group: {_ id: "$ category", count: {$ sum: 1 }}}, {$ Sort: {count:-1 }} ]); {"_ Id": "fruit", "count": 3} {"_ Id": "veggie", "count": 2}
|
The php code is as follows:
The code is as follows: |
Copy code |
$ Cond = array ( Array ( '$ Match' => array (' _ id' => array ('$ gt' => 0 )), ), Array ( '$ Group' => array ( '_ Id' =>' $ category ', 'Count' => array ('$ sum' => 1 ), ), ), Array ( '$ Sort' => array ("count" =>-1 ), ), ); $ Result = $ collection-> aggregate ($ cond ); Print_r ($ result); // The result is as follows: Array ( [Result] => Array ( [0] => Array ( [_ Id] => fruit [Count] => 3 ) [1] => Array ( [_ Id] => veggie [Count] => 2 ) ) [OK] => 1 )
|
Mongodb has many select operations. Here, we only talk about some common functions.