Mongodb group operation example in PHP
Dec 23, 2016 am 09:41 AMFollowing the previous article, this article mainly talks about the group function of mongodb, which is quite powerful. It is quite complicated to use find(), skip(), distinct(), etc.
Test data:
> 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 according to 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" ] } ]
php code is as follows:
$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); //结果如下。 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. Group according to category and 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 } ]
php code as follows:
$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); //结果如下。 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. Use aggregate group function, It’s also quite powerful
> 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:
$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); //结果如下: Array ( [result] => Array ( [0] => Array ( [_id] => fruit [count] => 3 ) [1] => Array ( [_id] => veggie [count] => 2 ) ) [ok] => 1 )
Mongodb has many select operations. Here, I only talk about some commonly used functions.
For more articles related to mongodb group operation examples in PHP, please pay attention to the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
