Example #4 MongoCollection::aggregate() with command options
To return information on how the pipeline will be processed we use the
explain command option:
$m= newMongoClient;$c=$m->selectDB("test")->selectCollection("zips");$pipeline= array(
array('$group'=> array('_id'=>'$state','totalPop'=> array('$sum'=>'$pop'),
),
),
array('$match'=> array('totalPop'=> array('$gte'=>10*1000*1000)
)
),
array('$sort'=> array("totalPop"=> -1),
),
);$options= array("explain"=>true);$out=$c->aggregate($pipeline,$options);var_dump($out);?>
以上例程的输出类似于:
array(2) {
["stages"]=>
array(4) {
[0]=>
array(1) {
["$cursor"]=>
array(3) {
["query"]=>
array(0) {
}
["fields"]=>
array(3) {
["pop"]=>
int(1)
["state"]=>
int(1)
["_id"]=>
int(0)
}
["plan"]=>
array(4) {
["cursor"]=>
string(11) "BasicCursor"
["isMultiKey"]=>
bool(false)
["scanAndOrder"]=>
bool(false)
["allPlans"]=>
array(1) {
[0]=>
array(3) {
["cursor"]=>
string(11) "BasicCursor"
["isMultiKey"]=>
bool(false)
["scanAndOrder"]=>
bool(false)
}
}
}
}
}
[1]=>
array(1) {
["$group"]=>
array(2) {
["_id"]=>
string(6) "$state"
["totalPop"]=>
array(1) {
["$sum"]=>
string(4) "$pop"
}
}
}
[2]=>
array(1) {
["$match"]=>
array(1) {
["totalPop"]=>
array(1) {
["$gte"]=>
int(10000000)
}
}
}
[3]=>
array(1) {
["$sort"]=>
array(1) {
["sortKey"]=>
array(1) {
["totalPop"]=>
int(-1)
}
}
}
}
["ok"]=>
float(1)
}