MongoCollection::aggregateCursor PHP Method

aggregateCursor() public method

Execute an aggregation pipeline command and retrieve results through a cursor
public aggregateCursor ( array $pipeline, array $options = [] ) : MongoCommandCursor
$pipeline array
$options array
return MongoCommandCursor
    public function aggregateCursor(array $pipeline, array $options = [])
    {
        // Build command manually, can't use mongo-php-library here
        $command = ['aggregate' => $this->name, 'pipeline' => $pipeline];
        // Convert cursor option
        if (!isset($options['cursor'])) {
            $options['cursor'] = new \stdClass();
        }
        $command += $options;
        $cursor = new MongoCommandCursor($this->db->getConnection(), (string) $this, $command);
        $cursor->setReadPreference($this->getReadPreference());
        return $cursor;
    }

Usage Example

Beispiel #1
0
function mkonereg($db, $colname, $grouparr)
{
    $ops = array();
    //  $ops[] = ['$match' => $query];
    //  $ops[] = ['$sort' => ['year'=> -1, 'month'=> -1]];
    $ops[] = ['$group' => $grouparr];
    $option = ['allowDiskUse' => true];
    //print_r($ops);
    $collection = new MongoCollection($db, $colname);
    echo "working on: {$colname} ... with";
    $col2name = $colname . "_reg";
    $col2 = new MongoCollection($db, $col2name);
    print_r($grouparr['_id']);
    makegrpIndex($db, $col2, $grouparr['_id']);
    //print_r($ops);
    try {
        $cursor = $collection->aggregateCursor($ops, $option);
    } catch (MongoException $e) {
        echo "error message: " . $e->getMessage() . "\n";
        echo "error code: " . $e->getCode() . "\n";
        exit(1);
    }
    //$results = $cursor['result'];
    foreach ($cursor as $result) {
        //print_r($result[_id]);
        $col2->insert($result['_id']);
    }
}
All Usage Examples Of MongoCollection::aggregateCursor