©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PECL mongo >=1.5.0)
MongoCollection::aggregateCursor — Execute an aggregation pipeline command and retrieve results through a cursor
$command
[, array $options
] )With this method you can execute Aggregation Framework pipelines and retrieve the results through a cursor, instead of getting just one document back as you would with MongoCollection::aggregate() . This method returns a MongoCommandCursor object. This cursor object implements the Iterator interface just like the MongoCursor objects that are returned by the MongoCollection::find() method.
Note: The resulting MongoCommandCursor will inherit this collection's read preference. MongoCommandCursor::setReadPreference() may be used to change the read preference before iterating on the cursor.
pipeline
The Aggregation Framework pipeline to execute.
options
Options for the aggregation command. Valid options include:
"allowDiskUse"
Allow aggregation stages to write to temporary files
"cursor"
It is possible to configure how many initial documents the server should return with the first result set. The default initial batch size is 101. You can change it by adding the batchSize option:
<?php
$collection -> aggregateCursor (
$pipeline ,
[ "cursor" => [ "batchSize" => 4 ] ]
);
This option only configures the size of the first batch. To configure the size of future batches, please use the MongoCommandCursor::batchSize() method on the returned MongoCommandCursor object.
"explain"
Return information on the processing of the pipeline. This option may cause the command to return a result document that is unsuitable for constructing a MongoCommandCursor. If you need to use this option, you should consider using MongoCollection::aggregate() .
"maxTimeMS"
Specifies a cumulative time limit in milliseconds for processing the operation (does not include idle time). If the operation is not completed within the timeout period, a MongoExecutionTimeoutException will be thrown.
Returns a MongoCommandCursor object. Because this implements the Iterator interface you can iterate over each of the results as returned by the command query. The MongoCommandCursor also implements the MongoCursorInterface interface which adds the MongoCommandCursor::batchSize() , MongoCommandCursor::dead() , MongoCommandCursor::info() methods.
Example #1 MongoCollection::aggregateCursor() example
Finding all of the distinct values for a key.
<?php
$m = new MongoClient ;
$db = $m -> test ;
$people = $db -> people ;
$people -> drop ();
$people -> insert (array( "name" => "Joe" , "points" => 4 ));
$people -> insert (array( "name" => "Molly" , "points" => 43 ));
$people -> insert (array( "name" => "Sally" , "points" => 22 ));
$people -> insert (array( "name" => "Joe" , "points" => 22 ));
$people -> insert (array( "name" => "Molly" , "points" => 87 ));
$ages = $people -> aggregateCursor ( [
[ '$group' => [ '_id' => '$name' , 'points' => [ '$sum' => '$points' ] ] ],
[ '$sort' => [ 'points' => - 1 ] ],
] );
foreach ( $ages as $person ) {
echo " { $person [ '_id' ]} : { $person [ 'points' ]} \n" ;
}
?>
以上例程的输出类似于:
Example #2 MongoCollection::aggregateCursor() example with different initial batch size
Finding all of the distinct values for a key.
<?php
$m = new MongoClient ;
$db = $m -> test ;
$people = $db -> people ;
$people -> drop ();
$people -> insert (array( "name" => "Joe" , "points" => 4 ));
$people -> insert (array( "name" => "Molly" , "points" => 43 ));
$people -> insert (array( "name" => "Sally" , "points" => 22 ));
$people -> insert (array( "name" => "Joe" , "points" => 22 ));
$people -> insert (array( "name" => "Molly" , "points" => 87 ));
$ages = $people -> aggregateCursor (
[
[ '$group' => [ '_id' => '$name' , 'points' => [ '$sum' => '$points' ] ] ],
[ '$sort' => [ 'points' => - 1 ] ],
],
[ "cursor" => [ "batchSize" => 4 ] ]
);
foreach ( $ages as $person ) {
echo " { $person [ '_id' ]} : { $person [ 'points' ]} \n" ;
}
?>
以上例程的输出类似于:
[#1] lukasz at endai dot com [2015-01-19 19:08:56]
It looks like maxTimeMS option in the latest release (1.5.8) is not valid anymore. Set \MongoCursor::$timeout before you call aggregateCursor()