Beispiel für einen Mongodb-Gruppenbetrieb in PHP
Im Anschluss an den vorherigen Artikel geht es in diesem Artikel hauptsächlich um die Gruppenfunktion von Mongodb, die ziemlich leistungsfähig ist. Die Verwendung von find (), skip (), unique () usw. ist ziemlich kompliziert.
Testdaten:
> 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. Gruppierung nach Kategorie
> 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" ] } ]
Der PHP-Code lautet wie folgt:
$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 . Nach Kategorie gruppieren und zählen
> 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 } ]
Der PHP-Code lautet wie folgt:
$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. Die Verwendung der Aggregatgruppenfunktion ist ebenfalls sehr leistungsfähig >
Der PHP-Code lautet wie folgt:> db.fruit.aggregate([ { $match: { _id: {$gt:0} } }, { $group: { _id: "$category", count: { $sum: 1 } } }, { $sort: { count: -1 } } ]); { "_id" : "fruit", "count" : 3 } { "_id" : "veggie", "count" : 2 }
$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 )
Weitere Artikel zu Mongodb-Gruppenoperationsbeispielen in PHP finden Sie auf der chinesischen PHP-Website!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

